A JS exception caused by 'form' element

167 Views Asked by At

Today I ran into this problem and it annoys me a lot... I'm maintaining a JS project and there's a line:

node.tagName

where node.nodeType is 1. Obviously the code wants to get the tag name of this element and it seems to work fine for 99.99% webpages...

Sadly, when I execute the script on http://codeforces.com/problemset/problem/377/D, it doesn't work. The reason is that there's a form element with a child node which has property name="tagName".
enter image description here

Child nodes of form element who have property name can be accessed like:node.NAME_VALUE(reference), so node.tagName will get its child whose name property is tagName rather than the node's tag name form.

Does anyone ever run into this problem, too? Are there any other solutions except checking whether the node is form?


EDIT 1:
I've fired a bug for jQuery here. I did this because it may be much easier for jQuery to fix this for .prop("tagName") than making all browsers solve this issue.
BTW, I think no one should use something like tagName or nodeName as an HTMLInputElement's name value.

2

There are 2 best solutions below

0
On

I'm no expert on these things, so this may not be the best answer you could get, but never the less... i can think of two solutions:

1) Use nodeName instead if you can be certain that input name will not exist.

2) Use element.clone(false).tagName (such as document.getElementById('someid').clone(false).tagName)

0
On

If node.tagName does not work for you, use node.nodeName.

I usenode.nodeName in my code.