i want to show hide multiple divs with jquery or javascript. I seached the web but i find codes that must have unique divs like:

<div id="div1">text</div>
<div id="div2">text</div>

and so on, but i want to add the divs in a foreache php function so i can't have multiple IDs, the foreache is a div wrapper. So, my question is how can i do that ?

Recommended Answers

All 15 Replies

Yes, like that, but i doesn't seem to work for me.
Is it because a use it in a .php file ?

Unlikely. Unless you are generating incorrect HTML. Hard to say without your code.

<div class="main">
    <h1>Liga 1 cu hide</h1>
    <?php 
    $itemlimit = 0;
    foreach ($liga1 as $item) {
        if ($itemlimit == 5) { break; }
    ?>
    <div class="flip">
        <?php if ($item->get_permalink()) echo $item->get_title(); ?>
    </div>
    <div class="panel">
        <?php 
        if ($item->get_permalink()) echo '<a href="' . $item->get_permalink() . '" ">'; 
        echo $item->get_title(); 
        if ($item->get_permalink()) echo '</a>';
        echo $item->get_description(); 
        ?>
    </div>
    <?php 
    echo $item->get_date('j M Y, g:i a');                       
    $itemlimit = $itemlimit + 1;
    }
    ?>
</div>

that is my code with div from the thread you gave me

yes, but i have to add some CSS too ?

CSS is only for looks. If all is there, then it should work. Is your page online somwhere for viewing?

"Warning: Invalid argument supplied for foreach() in /home/icehdnet/public_html/a/index.php on line 279"

Something wrong with your data, I guess it is empty. It does not output any div's so it can't work. You also copied the wrong Javascript snippet. Mine does not use #, and uses the next() function.

yes, it's empty but i don't think that's the problem because i deleted those lines and still doesn't work

You copied the wrong javascript snippet. You need this one:

$(".flip").click(function() {
    $(this).next().slideToggle(500);
});

i've tested both of them.. i saw in the other post the jsfiddler it does the work but i don't know why id doesn't in my script...

Updated with your function

You forgot something else, this should be it (completely this time):

<script type="text/javascript">
$(document).ready(function(){
  $(".flip").click(function(){
    $(this).next().slideToggle(500);
  });
});
</script>

Without the document ready function the code is executed too soon, and will not work.

You Are Legend, I'll post a link to your website as soon it will be ready.
Many Thanks !!

Not necessary, but if you want link to DaniWeb. Please consider to mark this question solved (button bottom left).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.