tags:

views:

42

answers:

3

I have a webpage from the Fogbugz bug tracking system, and using a plugin to Fogbugz, I can insert javascript into each page.

In this Fogbugz installation I have created a custom field, with the case number of a different system, and this is shown in the case listings.

I'd like to make this column clickable, which would open a new tab and open the corresponding case in that other system.

The table cell in the webpage looks like this in html:

<td class="col_8"><div onmousedown="x(event)"><nobr><span>44468</span></nobr></div></td>

Basically I'd like to make that number, 44468, into a clickable link.

I've tried this jQuery code:

$(".col_8").html("....");

but this replaces the contents of the entire TD cell. That's OK by me if that's the best way to do it, but I don't know how to refer to the case number so that the link becomes the right one.

This, for instance, works in the sense that they all become links, but creates the same link for all the cases (ie. they all point to the same case number, obviously):

$(".col_8").html("44468");

I improved it (I'm learning jQuery):

$(".col_8>div").html("<a href='http://other.system/?44468'&gt;44468&lt;/a&gt;");

Additionally, the first row has a text that denotes the name of that system, this has different html, and only letters in the text, and I'd like for that part to stay as it is.

Can anyone tell me where to look or what to google for in order to find out what I need?


Courtesy of @Nick Craver's answer and comments, this is the final script that worked for me:

$(".col_8 span").filter(function() {
    return $.trim($(this).text());
}).each(function() {
    $(this).wrap("<a target='_blank' href='http://other.system?" + this.innerHTML + "' />");
});
+4  A: 

You can pass a function to .wrap(), like this:

$(".col_8 span").wrap(function() {
  return "<a href='http://other.system/?" + this.innerHTML + "' />";
});

You can test it out here, the result is turning:

<span>44468</span>

Into:

<a href="http://other.system/?44468"&gt;&lt;span&gt;44468&lt;/span&gt;&lt;/a&gt;

The rest remains unchanged. If the markup isn't exactly as you posted and there are spaces, etc, you'll want $(this).text() instead of this.innerHTML, possibly with a $.trim() wrapper as well.


Since the OP is on jQuery 1.3.2 (the above is for jQuery 1.4+ only), here's that version:

$(".col_8 span").each(function() {
  $(this).wrap("<a href='http://other.system/?" + this.innerHTML + "' />");
});

You can test it here.

Nick Craver
Hm, I can't get that to work. This changes the text: `$(".col_8>div>nobr>span").html("lasse");`, this does not: `$(".col_8>div>nobr>span").wrap(function() { return "lasse"; });` Am I right in those two should do the same?
Lasse V. Karlsen
@Lasse - `.wrap()`'s function should return an element to wrap it in, if you did `.html(function() { return "lasse"; });` they'd be the same, but returning a string that's not an HTML element isn't what `.wrap()` is for :)
Nick Craver
@Lasse - You can also replace `.wrap()` with `.innerWrap()` if you want the `<a>` inside the `<span>`, whatever you're ultimately after.
Nick Craver
Could there be an old version of jQuery that I'm using? I'm using the one that comes with Fogbugz, but I don't know how to find the version number from the embedded source here.
Lasse V. Karlsen
@Lasse - Do `alert($.fn.jquery)` to see the version.
Nick Craver
Hm, ok, but the code you've shown doesn't do anything in my webpage. I've enabled the javascript console (Chrome browser), but there's nothing shown there either. I pasted it in verbatim, and there's no change.
Lasse V. Karlsen
It reports version 1.3.2.
Lasse V. Karlsen
@Lasse - Ah, then you're using an old version of jQuery, function support for wrapping was added in 1.4, you'll have to do this version instead: http://jsfiddle.net/nick_craver/WWafc/1/
Nick Craver
This worked, but there's another problem then, but that's my fault, I didn't consider that so I didn't mention it. Some of those spans are empty, but now I get a smallish link inside those as well. Is there a way for me to specify that only spans with content should be handled?
Lasse V. Karlsen
@Lasse - Are they totally empty, or may have spaces?
Nick Craver
They actually have a non-breakable space. I didn't notice that before. Not only am I posting jQuery questions, which is way outside my comfort zone, now I'm posting questions in the way I hate :P where the problem is divulged in bits and pieces as we go along.
Lasse V. Karlsen
@Lasse - Try this on for size: http://jsfiddle.net/nick_craver/WWafc/3/
Nick Craver
Magic, pure and utter magic. Thanks for helping me out like this, much appreciated.
Lasse V. Karlsen
@Lasse - welcome :)
Nick Craver
I had to modify it somewhat, a fly in the soup, the "col_8" name means "column nr. 8 in your current view", so any rearranging of the columns means it doesn't work as posted. I expanded the filtering though and it works again. I ended up with something that hits every `td>div>nobr>span`, to make sure it hits the columns, and then used a filter that looked at the contents, the id's in question are easily recognized as belonging to that other system.
Lasse V. Karlsen
@Lasse - No class on that column you can detect then use in the selector by chance is there? For example finding the header cell by it's text and taking it's index?
Nick Craver
+1  A: 

You can replace spans with anchors like this:

$('.col_8').each(function() {
    var $span = $(this).find('span');
    var id = $span.html();

    $a = $('<a />').attr('href', 'http://other.system/?'+id).html(id);

    $span.replaceWith($a);
});
Tatu Ulmanen
This worked nicely.
Lasse V. Karlsen
A: 

I haven't tested this, but hopefully it should get close to what you're after:

var spans = $('td.col_8 span'); // Find the spans within the table cells
if (spans && spans.length) { // If we have span tags to work with
  spans.each(function() {
    var span = $(this),
        code = span.text(), // Get the span tag's text node
        anchor;
    if (/^\d+$/.test(code) === true) { // If the text's characters are only digits
      anchor = $('<a href="http://other.system/?' + code + '">' + code + '</a>'); // Create an anchor tag
      span.replaceWith(anchor); // And replace the span with the anchor tag
    }
  });
}
dylanfm