6

In JQuery, how can i get list of input type with name, id and value present in any div?

Pradip
  • 1,317
  • 4
  • 21
  • 36

5 Answers5

18

Selects inputs that descend from a div that have both name, id, and value attributes present. Then pushes the "type" attribute of each matching item into an array.

var inputTypes = [];

$('div input[name][id][value]').each(function(){
     inputTypes.push($(this).attr('type'));
});

http://api.jquery.com/multiple-attribute-selector/

HandiworkNYC.com
  • 10,914
  • 25
  • 92
  • 154
6

In JQuery, how can i get list of input type...

This?

var inputTypes = [];
$('input[name!=""][value!=""][id!=""]').each(function() {
    inputTypes.push($(this).prop('type'));
});

The property!="" is necessary than just [property], this is to filter out things like

<input type="text" name="something" value="" id="someId" />

(Which I assume you don't want to get input with any property equals to empty string)

Andreas Wong
  • 59,630
  • 19
  • 106
  • 123
3

You can look at each input, get their attributes and check against an empty string for each. You could use any method you want to present them, here I'm just adding them to an array.

var inputs = new Array();  

$("input").each(function() {
    var name = $(this).attr("name");
    var id = $(this).attr("id");
    var val = $(this).val();

    if ((name) && name !== "") && ((id) && id !== "") && ((val) && val !== "")) {
        inputs.push(this);
    }
});
Paul Aldred-Bann
  • 5,840
  • 4
  • 36
  • 55
  • 1
    What if the ID is undefined? `undefined !== ""` is true, but it definitely shouldn't be in the list. – Anthony Grist Aug 16 '12 at 08:41
  • @AnthonyGrist very good point, I've updated my answer to check for a value & that it's not empty - reference this SO post: http://stackoverflow.com/questions/154059/what-is-the-best-way-to-check-for-an-empty-string-in-javascript – Paul Aldred-Bann Aug 16 '12 at 08:58
2

You can get access for input by:

$('input')

then get attributes which you want by for example .attr('type'), .attr('id'). And if you need list write them into array is $('input').each(...);

Mateusz Rogulski
  • 7,357
  • 7
  • 44
  • 62
0

When You want to select an input with an specific name like "foo" do it as below :

$('div input[name="foo"]').each(function(){
     // your program logic goes here
     // this.val("123")
});

Or you might wanna get ones which their value does not match "123" :

$('div input[value!="123"]').each(function(){
     // your program logic goes here
});

Or even a combination of both :

$('div input[name="foo"][value!="123"]').each(function(){
    // your program logic goes here
});

See JQuery API Reference for more details.

AmiNadimi
  • 5,129
  • 3
  • 39
  • 55