views:

445

answers:

5

I have got a table where i want to insert html in a td on the next row. I pasted one row below.

<tr class="content">
    <td align="center">
      <a class="version" href="theurl">click here to update the td in the row below</a>
    </td>
    <td align="left">col 2</td>
    <td align="left">col 3</td>
    <td align="left">col 4</td>
    <td align="left">col 5</td>
    <td align="left">col 6</td>
</tr>
<tr class="version" style="display: none;">
    <td colspan="6"></td>  <-- this is where i want to insert the new html in.
</tr>

my jquery script looks like this.

$("a.version").click(function () {
    var sLink = $(this).attr("href"); <-- this gets the link to retrieve data from
    var nexttr = $(this).parent().parent().next(".version"); <-- gets the next tr
    var nexttrtd = $(this).parent().parent().next(".version td:first"); <-- gets the first td in the next tr. alerting the colspan displays 6 (which is correct)

    nexttrtd.html("hello world"); <-- this wont insert the text in the td.
    nexttr.css("display", "");

    return false;
});

Now the question is, why doesn't the html get shown in the td? I have tried append, innerHTML = "bla" and that won't work either.

Think i'm doing things according the manual, but can't really figure out where it goes wrong.

any ideas?

+2  A: 

The next() probably fails as it tries to find a td:first element that's next to the current tr. Try this instead:

$("a.version").click(function () {
    var sLink = $(this).attr("href"); <-- this gets the link to retrieve data from

    var nexttrtd = $(this).closest('tr').next('.version').find('td:first');
    nexttrtd.html("hello world"); // This will insert "hello world" to the td

    return false;
});

I've reduced some redundant code that didn't seem to do anything. The weird thing is that you selected nexttr but then did the search for nexttrtd all over again from the start. You could have just used the nexttr element in aid:

var nexttr = $(this).closest('tr').next(".version"); // gets the next tr
var nexttrtd = nexttr.find('td:first'); // Gets the first td in that row
Tatu Ulmanen
don't forget `nexttr.show();` after "hello world" get put in the td
munch
Thanks! i put in the redundant code to make sure i got the right element. but chaining nexttr with .find maked it all work.it also fixed the second problem i wrote about (.html)
half-a-nerd
@half-a-nerd, if it solved your problem, don't forget to mark the answer as accepted :)
Tatu Ulmanen
A: 

I tried your code and it works for me. Is jQuery included properly in your page header, without any typo? I tested it with

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"&gt;&lt;/script&gt;
Maros Miskerik
A: 

Make sure your jQuery script is executed on the page. If it's not, the click event will never be added for your a tag.

rosscj2533
A: 

hi!

got one part working now. key to get the td in the next tr was the following:

var nexttrtd = $(this).parent().parent().next("tr.version, td"); $.get(sLink, function(sData){ nexttrtd.html(""+sData+""); nexttr.css("display", ""); });

(a comma in "tr.version, td") The thing now is that html doesn't insert the sData string IN the but IN STEAD OF the .

That's why i went around that by inserting " too but surely this isn't the way it should be or what?

Is there a proper way to do this?

(appending the sData will but the sData next to the so that is not wanted either.

half-a-nerd
+2  A: 

html doesn't insert the sData string IN the but IN STEAD OF the .

Use .append(), not .html()!

myfreeweb
yepp that worked, but only after i appended it to the element found with nexttr.find("td:first") in the original code the code would get inserted next to the <td> when using append.Thanks for your help.
half-a-nerd