-1

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!!

tonio
  • 10,355
  • 2
  • 46
  • 60
heika
  • 63
  • 2
  • 7

2 Answers2

1

Have you tried .getAttribute("commandArgument")?

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

Ben
  • 54,723
  • 49
  • 178
  • 224
1

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');
KARASZI István
  • 30,900
  • 8
  • 101
  • 128