How to add html table id using jquery

I am trying to create a html table ID using jQuery, which should be visible when I view the html page. For example, I have the following code and convert from the first table to another table with its ID, thanks.

<table>

</table>

      

FROM

<table id="tablecontent">

</table>

      

+3


source to share


3 answers


You need to put jquery (add id attribute) on page load.

Code to set id:



$('table').attr('id','test');

      

See the demo fiddle (just browse the element and look in the body in the results pane)

+4


source


you can try this:



$(document).ready(function(){
   $("table").attr("id", "tablecontent");
});

      

+1


source


Using jQuery attr () you can add an id for the table, this is where you need to consider which table you have selected to add the id. Try fiddle , in this example I re-validate the id to confirm it was added.

$('table').attr('id','idForTable');
var a = document.getElementsByTagName('table')[0];
alert($(a).attr('id'))

      

0


source







All Articles