3

Maybe I'm not understanding something basic here, but the html() call doesn't return an element with all attributes filled in, even after an explicit call to set the attribute.

For example, I have a text box:

<input type="text" id="foo" value="" />

When the user fills in a value in this box, a function is called (onBlur of input text box).

$('#foo').blur(function(event) { updateFoo(this); });

In that function the following code exists:

function updateFoo(input) {
  // force DOM update??
  $('#'+input.id).attr('value', input.value);

  // successfully displays value user entered
  alert($('#'+input.id).attr('value')); 

  // displays input element, with value not filled in,
  // like: 'input type="text" id="foo" value="" />'
  alert($('#'+input.id).parent().html()); 

  ...
}

Shouldn't the html() call return the element with the value attribute set?

I'm using Firefox 3.6.13 on Max OSX.

I saw this forum post: http://forums.asp.net/t/1578929.aspx which I based some of my assumptions off of...

Thanks, Galen

Galen
  • 491
  • 4
  • 15

2 Answers2

4

After researching some more, I ran across this post:

jQuery html() in Firefox (uses .innerHTML) ignores DOM changes

which pretty much sums up the problem I'm having. Basically it's a firefox implementation issue...

Instead of:

$('#'+input.id).attr('value', input.value);

I'm now using:

input.setAttribute('value', input.value); // force DOM update

which solved the problem.

Community
  • 1
  • 1
Galen
  • 491
  • 4
  • 15
2

I think this is a side effect of the fact that value has a very specialized use. I imagine that the value is stored in some local property instead of in the DOM itself.

If you change the attr to something else, like newattr, it works fine:

http://jsfiddle.net/m9vWT/

Of course, that doesn't help with the value attribute...

Jeff B
  • 29,943
  • 7
  • 61
  • 90
  • thanks for your response. Yes, it seems like new attributes get updated fine. I solved the problem in another way (see my answer in this post) – Galen Dec 10 '10 at 19:04