0

In IE > 7 it can be done by checking if value exists in the attributes collection or using getAttribute() which returns null, but in IE <= 7, this collection returns all the supported attributes and getAttribute() returns an empty string, the same as element.value.

attributes
getAttribute()

Any hints?

Gabriel Llamas
  • 18,244
  • 26
  • 87
  • 112
  • Why on earth do you want the `value` attribute instead of the `value` property? – Jeremy J Starcher Sep 14 '14 at 19:58
  • Form polyfill, specifically a select with required: http://stackoverflow.com/a/6048891/458093. If the first option has no "value" attribute but has text content, it validates, but if it has a "value" attribute with an empty string, it doesn't validate. Try it. – Gabriel Llamas Sep 14 '14 at 20:14

1 Answers1

0

In IE6/7, try checking for the attribute like this:

element["value"] !== undefined

Please see the first answer to this code review question for more details: https://codereview.stackexchange.com/questions/10131/will-this-test-to-check-if-an-element-has-an-attribute-work

Community
  • 1
  • 1
Peter
  • 2,654
  • 2
  • 33
  • 44
  • Did you try the routine mentioned in the post I linked to? Also, did you use `!==` or just `!=`? – Peter Sep 14 '14 at 20:12
  • Which routine? `el.value` returns an empty string, and yes, with strict comparator `!==` – Gabriel Llamas Sep 14 '14 at 20:16
  • If `el.value` is undefined, then that means that the value attribute is not present. I thought this is the behaviour you are after? – Peter Sep 14 '14 at 20:18
  • The function I am referring to is `return tag.hasAttribute ? tag.hasAttribute(attrName) : tag[attrName] !== undefined;`. I would also use IE7's debugging tools to inspect the element and verify that the attribute is not present in the DOM. Something else could be possibly creating this attribute in the background. – Peter Sep 14 '14 at 20:23
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/61225/discussion-between-peter-and-gabriel-llamas). – Peter Sep 14 '14 at 20:45