I am trying to use jQuery to loop through a list of elements that have the same classname & extract their values.
I have this..
JavaScript
x
16
16
1
function calculate() {
2
3
// Fix jQuery conflicts
4
jQuery.noConflict();
5
6
jQuery(document).ready(function(){
7
8
// Get all items with the calculate className
9
var items = jQuery('.calculate');
10
11
12
13
});
14
15
}
16
I was reading up on the each() function though got confused how to use it properly in this instance.
Advertisement
Answer
JavaScript
1
7
1
jQuery('.calculate').each(function() {
2
var currentElement = $(this);
3
4
var value = currentElement.val(); // if it is an input/select/textarea field
5
// TODO: do something with the value
6
});
7
and if you wanted to get its index in the collection:
JavaScript
1
4
1
jQuery('.calculate').each(function(index, currentElement) {
2
3
});
4