1

The code for the page is the following

<?php foreach ($organization->index() as $id=>$content) { ?>
<a href="#" id="obtain_branch"><?= $content->name?></a>
<div id="directory_brnach"><?= $content->about?></div>
<?php } ?>

The JavaScript code using jQuery is

 // Directory inner branches
$('#obtain_branch').click(function(){
   $('#directory_brnach').toggle('fast');
});

The problem it will only work on the first one given the the ID is not changing, the rest will not change.

How can I send an array to the jQuery function to give an individual ID on every iteration?

Thanks in advance.

2
  • Please edit your post and highlight your code, then press the "101010" button, so that it displays correctly. Commented Apr 11, 2010 at 9:44
  • um... what exactly are you trying to do? Also mark the code as code. It's easier to read. Commented Apr 11, 2010 at 9:45

2 Answers 2

2

The #obtain_branch selects the first element with that ID. Using a class will help for the links, but not for the divs - that will toggle them all at once. Try:

<?php foreach ($organization->index() as $id=>$content) { ?>
<a href="#" class="obtain_branch"><?= $content->name?></a>
<div class="directory_brnach"><?= $content->about?></div>
<?php } ?>

JavaScript:

$('.obtain_branch').click(function(){
    $(this).next('.directory_brnach').toggle('fast');
    return false; //to avoid the link from working
});
Sign up to request clarification or add additional context in comments.

Comments

2

IDs need to be unique. Try:

<?php foreach ($organization->index() as $id=>$content) { ?>
<a href="#" class="obtain_branch"><?= $content->name?></a>
<div><?= $content->about?></div>
<?php } ?>

with:

$('a.obtain_branch').click(function(){
   $(this).next().toggle('fast');
   return false;
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.