Reputation: 3860
I trying to get the all input elements having name="choices"
before submit. And i want to check the values of those elements before submiting the form .
The problem is instead of getting the 0 ,1 ,2 which is values of elements i'm getting undefined
(3 times).
form
<form action="/check" method="post" id="mform1" >
<div id="id_poll_choices" >
A). <input type="text" name="choices" value="0"><br>
B). <input type="text" name="choices" value="1"><br>
C). <input type="text" name="choices" value="2"><br>
</div>
<input type="submit" id="id_submit" value="Submit"/>
</form>
jquery
$('#mform1').submit(function(){
$( "input[name*='choices']" ).each(function(index, elm){
alert(elm.val)
});
});
alert showing undefined
.
what could be the issue here ?
Upvotes: 0
Views: 83
Reputation: 382150
elm
is a DOM element. It doesn't have any val
property.
Replace
alert(elm.val)
with
alert($(elm).val())
or
alert(elm.value) // better
A few notes :
Pay also attention to the id : only one element can have a given id.
You should use "input[name='choices']"
instead of "input[name*='choices']"
as selector. Both work here but one is faster and more selective. Unless your elements should have different names of course (which is the general practice apart for radio buttons)
this
in the callbackconsole.log
instead of alert
(read more)So I'd suggest this
$('#mform1').submit(function(){
$( "input[name='choices']" ).each(function(){
console.log(this.value);
});
});
Upvotes: 3
Reputation: 20313
Try:
$('#mform1').submit(function(){
$( "input[name*='choices']" ).each(function(index, elm){
alert(elm.value);
});
});
NOTE: Don't use same id's for all inputs. Id's should be unique.
Upvotes: 0