tags:

views:

242

answers:

1

I am not sure whether this only happens to me. Basically if I have a new element added to the page using YUI

var element = new YAHOO.util.Element(document.createElement('input'));
element.set('value', 'some value');
element.appendTo(document.body);

Then I read the value of the element using these different ways, and get consistent result

element.get('value'); // get 'some value'
element.get('element').getAttribute('value'); // get 'some value'
element.get('element').value; // get 'some value'
// jquery as reference
jQuery(element.get('element').val()); // get 'some value'

If I change the field value through the browser (for example, 'new value'), and repeat the above steps again, i get some interesting values

element.get('value'); // get 'some value'
element.get('element').getAttribute('value'); // get 'some value'
element.get('element').value; // get 'new value'
// jquery as reference
jQuery(element.get('element').val()); // get 'new value'

However, if the input element is already exists in the page and is currently holding user input -> 'some value'

var element2 = new YAHOO.util.Element('some_input');

First read get same result as above scenario

element.get('value'); // get 'some value'
element.get('element').getAttribute('value'); // get 'some value'
element.get('element').value; // get 'some value'
// jquery as reference
jQuery(element.get('element').val()); // get 'some value'

if the user enters 'new value' to the input field through the browser, then repeating the above steps gets

element.get('value'); // get 'new value'
element.get('element').getAttribute('value'); // get 'new value'
element.get('element').value; // get 'new value'
// jquery as reference
jQuery(element.get('element').val()); // get 'new value'

I am just wondering why would I get different values for dynamically generated element if different way of retrieving data is used, or it is just because I am doing it wrong?

+1  A: 

Old values popping up might be a caching issue.

I built your example using the latest YUI libraries here: http://tivac.com/yui2/jeffrey04_element.htm (you'll want to have Firebug open).

Using the correct

element.get('value');

calls is returning the proper value for me in both cases.


Not sure why

element.get('element').getAttribute('value')

is returning null sometimes though.

If you can narrow down the cases where it's returning an incorrect value I would create a simple test case and submit a bug. They really appreciate feedback like that and are generally fast to respond and fix issues.

Tivac
thanks, I am still using YUI2.6 so not sure if some of the issue fixed in the latest release. I do get null while calling DOM's getAttribute method as well. My test run resultelement.get('value'): "some value"element.get('element').getAttribute('value'): nullelement.get('element').value: "some value"element2.get('value'): "new value"element2.get('element').getAttribute('value'): "some value"element2.get('element').value: "new value"
Jeffrey04