Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery .val() does not work on html chunk?

I cant figure out, why this code does not work as expected:

var $obj = jQuery('<div>xx<input type="text" value="" />xx</div>');
$obj.find('input').val('testing');
console.log($obj.html());

The resulting output is without any change - i.e. no change in value. But append() and other functions works fine. What could be wrong?

like image 337
Luke Avatar asked Mar 23 '23 19:03

Luke


1 Answers

value is an atrribute of the input dialog.

Doing .val(...) changes the value property and not the value in the DOM.

See here for the differences between properties and attributes.


If you wanted to see a physical change in the value attribute you could do something like this:

var $obj = jQuery('<div>xx<input type="text" value="" />xx</div>');
$obj.find('input').attr('value','testing');
console.log($obj.html());

Demo: http://jsfiddle.net/maniator/YsZLt/

like image 158
Naftali Avatar answered Apr 02 '23 22:04

Naftali