1

I am trying to determine if the selected element is a input type element.

var element = this.getElement();
console(element) => <input type='text' name='input' value ='test'> or something else

How do I check if selected element is input element.

I searched google and someone said it can be

element.is('input') but it gave me error saying is undefined.

Can anyone help me here? Thanks a lot!

FlyingCat
  • 14,036
  • 36
  • 119
  • 198
  • 3
    `.is()` is part of jQuery. Try `element.nodeName.toLowerCase() === 'input'`. – gen_Eric Jul 15 '13 at 19:48
  • You can also use `toString()` method of the element: `console(element.toString())` --> `[object HTMLInputElement]` (if `element` was an `input` element). – Teemu Jul 15 '13 at 19:57
  • duck duck goose: inp.name && inp.type && inp.value!=null; this lets you hit all form inputs, not just . match type to hit specific types... – dandavis Jul 15 '13 at 20:04

1 Answers1

5

If element is a DOM node, you could use it's nodeName property.

var input = document.createElement('input')
input.nodeName; //returns "INPUT"

So, in your example, you could check if it is an input using element.nodeName === "INPUT"

Alternatively, you could do that with jQuery using $(element).is('input') - although I wouldn't use jQuery for such mundane tasks :)

Marlon Bernardes
  • 13,265
  • 7
  • 37
  • 44