tags:

views:

41

answers:

2

Sometimes passwordField.setAttribute("value","newValue") doesn't work on my password field. But passwordField.value = "newValue" always works.

moreover, setAttribute is working fine on Mac platform but sometimes doesn't work on win platform

Why is this happening ?

A: 

.setAttribute() does not work in Internet Explorer. I suggest using a framework like jQuery for DOM-manipulation, since it works cross-browser. This is the jQuery-way of doing this:

$(passwordField).val('newValue');

Take a look at the getting started guide.

elusive
jQuery is overkill for this when the `value` property works perfectly.
Tim Down
Thats right, but since jQuery fixes a lot of DOM-related compatibility problems, this might be useful anyway. `getElementById()` is about the only DOM-method that is cross-browser. This is a mess without normalization.
elusive
+1  A: 

Forget about attributes for HTML elements in JavaScript. There are issues in IE, they don't do exactly what you think and are unnecessarily verbose. The value property does exactly what you need and works correctly in all scriptable browsers.

var input = document.getElementById("your_input");
input.value = "blah";
Tim Down