Question

In the web page, I have some codes like

<span commandArgument="a">A</span>
<span commandArgument="b">B</span>

I want to use javascript to get the commandArgument values of the spans, and the code I wrote is

var spans = document.getElementsByTagName("span");
var value = spans[0].commandArgument;

It works in IE but fails in Firefox.
IE gets value = "a" and Firefox gets value = undefined.
Is there any method to get the values that works in both browser? Thanks!!

Was it helpful?

Solution

You can use the getAttribute() from the methods of the DOM element.

This method works in old browsers as well according to the compatibility sheet of quirskmode.org

var value = spans[0].getAttribute('commandArgument');

OTHER TIPS

Have you tried .getAttribute("commandArgument")?

...and its ugly cousin must be mentioned, .setAttribute("commandArgument", "c")

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top