New here? Start with the essentials and go from there.

Wednesday, July 2, 2008

IE Fun Fact: getAttribute of form action doesn’t work

Given the following HTML:
<form id="form" action="servlet.do">
  <input type="submit" name="action" value="Add" />
</form>
How does one get the value of the form’s action attribute? form.action is of course right out, as that returns the <input> node. But what about:
var form = document.getElementById('form');
var action = form.getAttribute('action');
Makes sense, no? It turns out that this doesn’t work in Internet Explorer. In IE, the result of the getAttribute call is still the <input> node.

The solution, as described in this forum (thx, MHenke!) is to dig into your DOM reference book and use getAttributeNode instead:
var form = document.getElementById('form');
var action = form.getAttributeNode('action').value;
And good times were had by all.

0 comments:

Post a Comment