In JQuery, how can i get list of input type with name, id and value present in any div?
-
Ozzone could you post a snippet of HTML and which elements you wish to select from it? – Michael Lloyd Lee mlk Aug 16 '12 at 08:44
-
Elclanrs, To me this question is perfectly clear. If you need clarification, you should ask. Your comment is not constructive because it does not indicate *why* – HandiworkNYC.com Aug 16 '12 at 08:44
5 Answers
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'));
});

- 10,914
- 25
- 92
- 154
-
-
3@undefined I mean the question is asking for inputs with a name AND an id AND a value, but the code in the answer returns inputs with a name OR an id OR a value. – Anthony Grist Aug 16 '12 at 08:42
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)

- 59,630
- 19
- 106
- 123
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);
}
});

- 5,840
- 4
- 36
- 55
-
1What 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
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(...);

- 7,357
- 7
- 44
- 62
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.

- 5,129
- 3
- 39
- 55