Repeat function for every div on page
How would i repeat a function for every div that has an id of lets say of 1.
This is what my html would look like.
<div class="main" id="1">
<div class="select" id="1">value</div>
<div class="select" id="2">value</div>
<div class="select" id="3">value</div>
<div class="select" id="4">value</div>
<div class="select" id="5">value</div>
</div>
<div class="main" id="1">
<div class="select" id="1">value</div>
<div class="select" id="2">value</div>
<div class="select" id="3">value</div>
<div class="select" id="4">value</div>
<div class="select" id="5">value</div>
</div>
i want to loop through all div's that have the class main and get the value of all the cub classes called select. i keep adding to the page so there could be one main div or there could be 100.
Thanks so much
EDIT: what about something like this.
My html:
<div id="status">开发者_Python百科</div></div>
<div class="main">
<div class="select">value</div>
<div class="select">value</div>
<div class="select">value</div>
<div class="select">value</div>
<div class="select">value</div>
</div>
<div class="main">
<div class="select">value</div>
<div class="select">value</div>
<div class="select">value</div>
<div class="select">value</div>
<div class="select">value</div>
</div>
And my jquery:
$('.main div:nth-child(1)').each( myFunction );
function myFunction()
{
var value = $(this).val;
$('#status').append(value);
}
for each div with the name main i want to have a function that will get the values of ever div with the class select on the main div its on. and then move on to the next main class.
A valid HTML markup
needs to have unique ids. So don't use multiple ID's. Use classes instead, lets assume you have a class called one
assigned to multiple elements:
$('.one').each(function(){
$(this).somefunction();
});
That code would query all those elements and call a method somefunction()
on each of them.
See .each()
You can't have multiple same id's! Also id cannot be number-starting. Add class like "c1", then
$('.main .select').each(function(el){
alert($(this).val());
});
I'm new to jQuery, so check
Misiur was close but the each method just takes a function with the index and element. Also I added the div tag before the classes so that it doesn't look at all elements. Just makes it a little more efficient.
$('div.main div.select').each(function(index, el){
alert($(this).val());
});
You probably don't need numbers on each div.
$('.main div:nth-child(1)').each( myFunction );
function myFunction()
{
//operate on $(this)
}
精彩评论