views:

1791

answers:

5

I'm hand-maintaining an HTML document, and I'm looking for a way to automatically insert a link around text in a table. Let me illustrate:

<table><tr><td class="case">123456</td></tr></table>

I would like to automatically make every text in a TD with class "case" a link to that case in our bug tracking system (which, incidentally, is FogBugz).

So I'd like that "123456" to be changed to a link of this form:

<a href="http://bugs.example.com/fogbugz/default.php?123456"&gt;123456&lt;/a&gt;

Is that possible? I've played with the :before and :after pseudo-elements, but there doesn't seem to be a way to repeat the case number.

+6  A: 

Not in a manner that will work across browsers. You could, however, do that with some relatively trivial Javascript..

function makeCasesClickable(){
 var cells = document.getElementsByTagName('td')
 for (var i = 0, cell; cell = cells[i]; i++){
  if (cell.className != 'case') continue
  var caseId = cell.innerHTML
  cell.innerHTML = ''
  var link = document.createElement('a')
  link.href = 'http://bugs.example.com/fogbugz/default.php?' + caseId
  link.appendChild(document.createTextNode(caseId))
  cell.appendChild(link)
 }
}

You can apply it with something like onload = makeCasesClickable, or simply include it right at the end of the page.

Dan
Careful about blindly wrapping those TD contents in links; block-level elements might show up, and they're not valid inside A tags. Also, I'd find the right table and get its child elements and not just scoop up all the TDs in the document.
Kent Brewster
+2  A: 

Not possible with CSS, plus that's not what CSS is for any way. Client-side Javascript or Server-side (insert language of choice) is the way to go.

da5id
A: 

I don't think it's possible with CSS. CSS is only supposed to affect the looks and layout of your content.

This seems like a job for a PHP script (or some other language). You didn't give enough information for me to know the best way to do it, but maybe something like this:

function case_link($id) {
    return '<a href="http://bugs.example.com/fogbuz/default.php?' . $id . '">' . $id . '</a>';
}

Then later in your document:

<table><tr><td class="case"><?php echo case_link('123456'); ?></td></tr></table>

And if you want an .html file, just run the script from the command line and redirect the output to an .html file.

yjerem
+4  A: 

here is a jQuery solution specific to your HTML posted:

$('.case').each(function() {
  var link = $(this).html();
  $(this).contents().wrap('<a href="example.com/script.php?id='+link+'"></a>');
});

in essence, over each .case element, will grab the contents of the element, and throw them into a link wrapped around it.

Owen
A: 

You could have something like this (using Javascript). Inside <head>, have

<script type="text/javascript" language="javascript">
  function getElementsByClass (className) {
    var all = document.all ? document.all :
      document.getElementsByTagName('*');
    var elements = new Array();
    for (var i = 0; i < all.length; i++)
      if (all[i].className == className)
        elements[elements.length] = all[i];
    return elements;
  }

  function makeLinks(className, url) {
    nodes = getElementsByClass(className);
    for(var i = 0; i < nodes.length; i++) {
      node = nodes[i];
      text = node.innerHTML
      node.innerHTML = '<a href="' + url + text + '">' + text + '</a>';
    }
  }
</script>

And then at the end of <body>

<script type="text/javascript" language="javascript">
  makeLinks("case", "http://bugs.example.com/fogbugz/default.php?");
</script>

I've tested it, and it works fine.

Vincent McNabb