1

I would know if this is a bug... When I alert html content from #test, I get :

<input name="sum" value="" type="text">

whereas it was set to 55 just before the alert, and I can view it on the brower.

Can you tell me why ? :-)

    <div id="test">
    <input type="text" name="sum" value="">
</div>

<script language="javascript">
 $(document).ready(function() {
    $("#test").find("input[name='sum']").val(55);
    alert($("#test").html());
 });
 </script>
1
  • Welcome to StackOverflow, Tom! Commented Jan 19, 2010 at 0:26

2 Answers 2

3

Javascript doesn't change the physical markup on the page. It changes the DOM. The DOM is constructed from the markup, but after that the markup isn't crucial.

You don't want to alert the HTML, you want to alert the value of the input:

alert($("#test :input[name='sum']").val());

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

Comments

1

This is not a bug. The value of #test is not part of the HTML content of the input -- it was set after the element was inserted into the DOM -- and thus the html() function doesn't return it.

4 Comments

Ok thanks, I thought that html() function would change according to updates from children...
If you'd added more elements (for instance) inside the div, html() would return them, but changing attribute values is not reflected. I think.
Is there a way to see how the source looks really like ? Thanks !
That's kind of a tricky question. Arguably, the source of the page doesn't include the value that you've set later. But to answer your question more helpfully, stackoverflow.com/questions/1388893/… is another discussion of this issue, which provides a jQuery plugin you could use.

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.