Reputation: 38180
I'm total beginner in jquery.
I tried this https://jsfiddle.net/0xLqhufd/2/
<body>
<div>
<input type="text" person="firstName" value="John">
<input type="text" person="birthDate" value="September 1st, 2000">
</div>
</body>
But I got only blank in alert :
$('input[person]').each(function(index) {
alert(index + ': ' + $(this).text());
}
);
Upvotes: 0
Views: 933
Reputation: 314
As said on the jQuery documentation ...
The .text() method cannot be used on form inputs or scripts
Use .val() instead.
$('input[person]').each(function(index) {
alert(index + ': ' + $(this).val());
}
);
Upvotes: 2
Reputation: 231
Hey you can use like this:-
input "type=text" tags have the values not text.
that's why you have to use .val() instead of .text().
$('input[person]').each(function(index) {
alert(index + ': ' + $(this).val());
});
Upvotes: 1
Reputation: 4210
I have a answer for your Doubt.
You have to change the Script which you have used as below.
$('input[person]').each(function(index) {
alert(index + ': ' + $(this).val());
}
);
Please update me whether you find my code is useful for you.
Upvotes: 1
Reputation: 28513
input has .val() to read values
$('input[person]').each(function(index) {
alert(index + ': ' + $(this).val());//will read input value
}
);
Upvotes: 1
Reputation: 67207
In void elements, using text()
would be inappropriate. Use .val()
instead. input[type=text]
is a void element, that means it cannot have any child nodes inside of it including text nodes
.
$('input[person]').each(function(index) {
alert(index + ': ' + $(this).val());
});
Upvotes: 1