3

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

<table>

</table>

TO

<table id="tablecontent">

</table>
4
  • 1
    JQuery updates the DOM (right click -> inspect element), not the page's source HTML; this can't be done, unless you were mistaken about 'page source,' and you meant the DOM). Commented Sep 30, 2014 at 9:37
  • why i cannot see ?? is there not any other way to add class which should be visible? Commented Sep 30, 2014 at 9:38
  • I think there's maybe a translation problem? The source HTML is the HTML text (<doctype html><html>...</html>) sent by the server, the DOM is what the browser constructs to represent that HTML source and manipulate it. The page is the representation you can see in the viewport of the browser. Commented Sep 30, 2014 at 9:41
  • iam using google chrome i cannot see viewport option, which browser should i use to view? thanks. Commented Sep 30, 2014 at 9:46

3 Answers 3

7

You need to put the jquery (adding attribute of id) on page load.

Code to set id:

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

See the demo fiddle (Just inspect element and look in the body in the result panel)

Sign up to request clarification or add additional context in comments.

3 Comments

you wanted the id on the table element, in the demo just do a right click on 'test' and do an inspect element. It is wrapped with the table tag having the id set from jquery
If i have multiple tables its changing all tables. How can i set id to specific or selected table?
then you need to build logic built on classes, or loop on multiple tables
1

you can try this:

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

Comments

0

Using jQuery attr() you may add the ID for table, here need to consider the which table you pick to add an ID. Try this fiddle, on this example I alert the ID again to confirm that it has been appied.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.