$(document).ready(function() { $('#...">

Combining jQuery with PHP

I have a simple jQuery toggle script like this:

<script type="text/javascript">
$(document).ready(function() {
  $('#clickedit').click(function() {
    $('#st').toggle();
  });
});
</script>

And of course in my HTML I have

<div id="clickedit">CLICK ITEM TO TOGGLE</div>
<div id="st">content to show/hide</div>

Now ... If I work with PHP and I repeat several "elements", and each "element" has its own content to show / hide, I can not set static identifiers for my divs, because the script simply will not work. I can assign the element id to my divs (something like echo "<div id='clickedit".$id."'>";and echo "<div id='st".$id."'>";), but I don't know how to handle them in my jQuery script! I just open jQuery and it is brilliant but still confusing for me :) So any help would be great!
Thanks in advance!

+3
source share
2 answers

, - :

<div class="clickedit" id="clickedit123">CLICK ITEM TO TOGGLE</div>
<div class="st" id="st123">content to show/hide</div>

, , , .

$('.clickedit').click(function() {
    var id = this.id.replace('clickedit', '');
    $('#st' + id).toggle();
}

, , .

+2

jQuery!

<script type="text/javascript">
$(document).ready(function() {
  $('.div1').click(function() {
    $(this).next('.div2').toggle();
  });
});
</script>

PHP , :

echo "<div class='div1' id='clickedit".$id."'>"; and echo "<div class='div2' id='st".$id."'>";
+1

Source: https://habr.com/ru/post/1773332/


All Articles