I have a page where the input type always varies, and I need to get the values depending on the input type. So if the type is a radio, I need to get which is checked, and if it is a checkbox I need to now which are checked, and if it is a drop down I need to know which is selected, and I if it is a text/textarea I need to know the values.

Any idea on how to do that?

+2  A: 

You could do the following:

var inputType = $('#inputid').attr('type');
does this means that I have to give each radio/check the same id?
@zeina - *Never* give elements the same ID.
patrick dw
@patrick- I know that ids must be unique, yet why are they suggesting to get the input type by id, and I may have radios and check box in which i shall depend on their names?!
NO you cannot give them the same ID, that is NOT valid. you can use the :input jQuery selector
Mark Schultheiss
+2  A: 
+1  A: 
GetValue = function(id) {
  var tag = this.tagName;
  var type = this.attr("type");

  if (tag == "input" && (type == "checkbox" || type == "radio"))

  return this.val();
Matthew Abbott
+1  A: 

The best place to start looking is

This will give you a good set of examples.

Ultamatly the selecting of elements in the DOM is achived using CSS selectors so if you think about getting an element by id you will want to use $('#elementId'), if you want all the input tags use $('input') and finaly the part i think you'll want if you want all input tags with a type of checkbox use $('input, [type=checkbox])

Note: You'll find most of the values you want are on attributes so the css selector for attributes is: [attributeName=value]

Just because you asked for the dropdown as aposed to a listbox try the following:

$('select, [size]).each(function(){
  var selectedItem = $('select, [select]', this).first();

The code was from memory so please accound for small errors

+4  A: 

get all the inputs:

var allInputs = $(":input");

get all the inputs type:


get the values:


NOTE: .val() is NOT the same as :checked for those types where that is relevent. use:


to get the value of the check - whatever it is currently. or simply use the ':checked' if you want only those that ARE checked.

EDIT: Here is another way to get type:

var allCheckboxes=$('[type=checkbox]');

EDIT2: Note that the form of:


is perferred over


which both equate to:


but the "input" is desired so it only gets the inputs and does not use the universal '*" when the form of $(':radio') is used which equates to $('*:radio');

Mark Schultheiss
I tried this:var type=$(":input[name="+name+"]").attr('type');and it worked!Thanks.
+1  A: 
var val = $('input:checkbox:checked, input:radio:checked, \
   select option:selected, textarea, input:text',


  1. I assume, that there is exactly one form element, that can be either a textarea, input field, select form, a set of radio buttons or a single checkbox (you will have to update my code if you need more checkboxes).

  2. The element in question lives inside an element with ID container (to remove ambiguences with other elements on the page).

  3. The code will then return the value of the first matching element it finds. Since I use :checked and so on, this should always be exactly the value of what you're looking for.

+1  A: 

If what you're saying is that you want to get all inputs inside a form that have a value without worrying about the input type, try this:


var $inputs = $('form').find(':checked,:selected,:text,textarea').filter(function() {
    return $.trim( this.value ) != '';

Now you should have a set of input elements that have some value.

You can put the values in an array:

var array = ${
    return this.value;

Or you could serialize them:

var serialized = $inputs.serialize();
patrick dw