i've never looped through elements with jquery and it would be great with some help.
my DOM looks like:
<div class="section">
<div class="group">
<div class="comment">
<input class="comment" type="text" />
<br />
</div>
<div class="link">
<input class="link" type="text" />
<input class="link" type="text" />
<br />
</div>
</div>
<div class="group">
<div class="comment">
<input class="comment" type="text" />
<input class="comment" type="text" />
<br />
</div>
<div class="link">
<input class="link" type="text" />
<br />
</div>
</div>
</div>
how do i write the code to get all values in text input fields (class=comment and class=link). there will be lot of groups with different numbers of text input fields.
thanks!
$(":input.comment, :input.link").each(function() {
alert($(this).val()); // or this.val
});
See:
This selects all elements with a class of comment or link, and alerts its value.
$(".comment, .link").each(function() {
alert($(this).val());
});
Alternatively, you could select on the input type:
$("input[type='text']").each(function() {
alert($(this).val());
});
The following syntax is clearer to me (although it's functionally equivalent to the other answers):
var elementList = $(":input.comment, :input.link");
$.each(elementList, function(i, input){
alert($(input).val());
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With