1

I'm simply setting a custom attribute in the document.ready function in the following way:

$(document).ready(function(){
    $("div.para, pre.screen, div.figure, pre.programlisting").attr("data-test-ID", "hello");
});

When I reload the HTML and look at the source, there is no trace of such attribute anywhere. Any ideas? I'm really stuck on this and can't figure it out.

3
  • 1
    Look in the DOM (with inspect element in your browser's context menu) the HTML source of the page isn't, and can't, be touched by JavaScript. Commented May 12, 2013 at 15:41
  • It would never show up. Look in the console of chrome instead. The set .data("testid") instead Commented May 12, 2013 at 15:41
  • @DavidThomas, ah, you're of course right... Silly me. Commented May 12, 2013 at 15:44

1 Answer 1

2

This in fact works, you have to look at the DOM (Document Object Model) as any changes done by javascript do not directly edit the page source. the source will always be exactly what the server sent.

$(document).ready(function(){
    $("div.para, pre.screen, div.figure, pre.programlisting").attr("data-test-ID", "hello");
    $("#result").text("para contains :" + $("div.para").attr("data-test-ID"));//view value
});

Working Example

Using Chrome or Firefox, Right Click on the element and click Inspect Element to view the values set by the DOM to the element

You code is correct and the value is infact set :)

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

2 Comments

Thanks for a thorough answer. Hopefully this will serve future questioners to avoid silly mistakes :)
Your welcome! Silly mistakes happen to all, sometimes all we need is a helping hand :)

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.