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>
Tom
  • 257
  • 1
  • 3
  • 9

2 Answers2

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());

Sampson
  • 265,109
  • 74
  • 539
  • 565
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.

eliah
  • 2,267
  • 1
  • 21
  • 23
  • Ok thanks, I thought that html() function would change according to updates from children... – Tom Jan 19 '10 at 00:27
  • If you'd added more elements (for instance) inside the div, html() would return them, but changing attribute values is not reflected. I think. – eliah Jan 19 '10 at 00:37
  • Is there a way to see how the source looks really like ? Thanks ! – Tom Jan 19 '10 at 00:38
  • 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, http://stackoverflow.com/questions/1388893/jquery-html-in-firefox-uses-innerhtml-ignores-dom-changes is another discussion of this issue, which provides a jQuery plugin you could use. – eliah Jan 19 '10 at 00:55