0

I'm sure this has been answered here but I just don't know how to word the question. I am getting all of the elements of a form using:

//get all form elements
$("#" + thisForm + " :input").map(function () { 

//get element data
var elementName = $(this).attr('name');
var elementType = $(this).attr('type');

and then storing them in a javascript object. The problem is that select and textarea elements do not have a type, so they appear as undefined. Is there a way to filter for them using the :input").map function or do I have to go to something like this:

$(thisForm + " > fieldset > select").add(thisForm + " > fieldset > textarea").each(function() {
  // do stuff
});

Thanks

2
  • 1
    I am unsure what you want to achieve here. Commented Jun 1, 2011 at 16:44
  • I'm getting all the elements from a form. I'm then looking to sort those elements by type (radios, checkboxes, selects). However, select and textarea do not have a type attribute so the way I am currently doing it returns elementType as undefined. As I am mapping over all the elements how can I determine it's type? Commented Jun 1, 2011 at 17:10

1 Answer 1

1

If the returned elementType is undefined, you can try getting the tagName property (plain Javascript HTMLElement property) which will return the name of the tag itself (like select, textarea, etc.).

So you can do something like:

var elementType = $(this).attr('type');
if (typeof elementType == 'undefined') {
    elementType=this.tagName.toLowerCase();
}

jsFiddle Demo

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.