views:

49

answers:

3

Hello Everyone, I am very new to jquery/javscript. I have a simple question.

Lets say i have a tag like this:

<Table id="mytable" >
<Table>

And i want to add some rows to this table.

Then we do like:

$("#mytable").append("<tr><td>value</td></tr>");
      //some thing like this may not be having the right syntax

Now my question is lets the id of the table is in a variable for example:

var table="mytable"; //which is coming from the back end

Now my using the "table" variable how can I append the row..?

Is that going to be

$("#"+table).append("<tr><td>value</td></tr>");//

Can some one help me out in this simple thing? Thanks, Swati

+1  A: 

that's correct!

var tableId = 'myTable';
$('#' + tableId).append('your row');

will append whatever you put in the 'your row' to the table with the id 'myTable'

Patricia
+1  A: 

That's exactly how you would do it. The selector is only expecting a string, so any form of concatenation or string logic via ternary operators will do the trick. This is a very powerful feature of jQuery selectors.

bkuhns
+1  A: 

Yes, you can reference an ID like that by storing a variable in advance. This would append table rows/cells. You may need to dig deeper between the rows and cells depending on what you need to do though. :)

$(document).ready(function () {
    var table = "myTable";

    $("#" + table).append("<tr><td>row1</td><tr>");
});
Delebrin