2

Is there a way using Jquery or just plain javascript that you can derive if something is a form field? Example

<div id='some_id'></div> 

or:

<input type='text' id='some_id'> 

Is there a way to check $('#some_id') to ensure it is actually a valid form field type like input, select, checkbox, radio, etc. and not a div, td or other element?

0

2 Answers 2

9

jQuery has an :input selector that matches form fields (input, textarea, select, etc.)

$(el).is(':input') 

Where el is a selector or a DOM element.

Sign up to request clarification or add additional context in comments.

Comments

3

You can use is() to find out the elements type :

if ( $('#some_id').is('input') ) { // it's an input element } if ( $('#some_id').is('div') ) { // it's a div } 

you can even check for multiple types :

$('#some_id').is('input, select, textarea, button'); 

FIDDLE

11 Comments

That won't match textarea or select.
Why not change input to :input...
@SLaks - Just add more elements to the selector then ?
@marteljn - I just always try to stay away from jQuery's pseudo selectors as much as possible, and certainly in a selector without any context.
Don't forget button :)
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.