Getting a button element's value attribute (not innerHTML) in IE
Published: • Read time: 2 min read min
Table of Contents
After spending a small part of my evening debugging Javascript in IE (which is ALWAYS a pleasure), I found out one of my errors was a mistake I had made before⦠trying to access button.value in IE. IE, of course, being IE, returns the innerHTML value of the button, instead of the value attribute. Last time I ran into this, I used a class instead of value, and moved on with my life. Tonight, I was feeling stubborn, and I found a better wayā¦
target.value = target.getAttributeNode(āvalueā).nodeValue;
Iām sure Iām about the millionth person to discover this, but I couldnāt find it anywhere using standard searches, so I thought Iād try to emphasize it here so others could find it. (Hopefully itās not so common that everyone else knows it!)
At first, I used the following:
target.value = target.attributes.getNamedItem(āvalueā).nodeValue;
Then I looked at Flanaganās Javascript: The Definitive Guide (using his amazon associates link), where he states that IE implementation of the attributes array,āmakes it impossible to use this feature portably.ā He doesnāt mention which version of IE (this specific line of code worked in IE6, IE7, and IE8a), but I figured Iād go with the more general version.
If you read this, I hope I could save you a bit of time.
P.S. ā I used IE8aās Debugger to help. Hereās hoping they develop it further before the standard release. Itās MUCH better than flying blind, but I canāt imagine a less helpful message than specifying an object in the console, and seeing ā{ā¦}ā.