javascriptcommandargument

Getting command argument in javascript


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


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');