Welcome3 |
Wysłany: Pon 16:20, 06 Gru 2010 Temat postu: ghd Quirky jQuery - Setting the 'value' attribute |
|
<input>
<input>
Quirky jQuery - Setting the 'value' attribute in HTML Quirky jQuery - Setting the 'value' attribute in HTML Posted October 17th, 2008 by Chris
This one stumped the guys over at the jQuery IRC channel:
When you use the jquery to set an element's value attribute, you can do the folloearng:
$(this).attr('value', 'newvalue');
or this:
$(this).val('value');
For most attributes,ghd, this will change the actual html. To test it out, you can use Firebug and watch the value change, or print out the new output by doing something like:
alert($(this).parent().html());
For the 'value' attribute, however, the html doesn't actionually change. And that little quirk cost me a few hours in research to try to find a toil-around. What I was trying to do was cache a bock of html in a database, but I wanted to retain the value of the text inputs if they changed. Since the 'value' attribute didn't actually deviate in the html, I got stuck. Here's what I did to work around it:
Whenever the text changed in the text input,ghd prices, I would create a hidden input with the new value. When the text changed again,ghd hair products, the hidden input would be deleted and then added back in. I tried doing this with just the text input (i.e., deleting it, manually adding the value in, and re-outputting it), but when you do that you lose any event bindings. When I went to cache the html,ghd hair dryer, it would look like this:
Since this is what will be pulled from the database, next I need to trigger a switch to replace out the old value of the text input with the value in the hidden input. That's easy enough with jQuery, since we have a 'rel' attribute to link them.
Personally,ghd straighteners, I believe this is an ugly work-around. Even changing the 'value' attribute with straight javascript didn't seem to work. So, this is really a javascript quirk I'd love to find a better step around. |
|