Hi,
I am trying to create toggle buttons for a page with profiles in. I have around 60 buttons on the page and want to try not to define the click function 60 times:
$(document).ready(function(){
$(".button1").click(function(){
$(".1").toggle();
});
$(".button2").click(function(){
$(".2").toggle();
});
$(".button3").click(function(){
$(".3").toggle();
});
$("p").css({display: "none"});
});
So I was trying to do a for each loop with JQuery so that I would only have to declare it once for all the buttons but I can't get it to work (please note JQuery is stored on the server and so I do not need to reference the google libraries within the code I am displaying and I have got the buttons above to work fine).
So below is the code that I am trying to do this with but I am not sure how to use the .each(function() which I saw on the JQuery website with what I am trying to do below:
<script type="text/javascript">
for each (i = 1; i < i.length; i++;)
{
$(".button[i]").click(function(){
for each(j = 1; j < j.length; j++;)
{
$(".[j]").toggle();
});
}
</script>
<div>
<button style="position: relative; float: right; top: -45px;" class="button1">Show more/hide</button>
<p class="1">
my text here
</p>
</div>
<div>
<button style="position: relative; float: right; top: -45px;" class="button2">Show more/hide</button>
<p class="2">
my text here
</p>
</div>
Any help on this would be appreciated.