Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

kendrick's avatar

onclick in a foreach loop

I have a simple open() function to trigger a hidden div by an id, and display block the content:

function open() {
	var x = document.getElementById("open");
	if (x.style.display === "none") {
	x.style.display = "block";
	} else {
		x.style.display = "none";
	}
}

@foreach($users as $user)
<div class="button" onclick="open()">
       <h4>+</p> 
</div> 
<div id="open">
	<h4>{{$user->name}}</p> 
</div>
@endforeach

Logically this only works for one element (the first) in a foreach loop. How can I make my method more dynamic, so that if I have a collection of e.g. 5 elements - I can open each element separately?

0 likes
17 replies
bugsysha's avatar

Just pass element as a parameter to open function.

bugsysha's avatar
function open(x) {
	if (x.style.display === "none") {
	x.style.display = "block";
	} else {
		x.style.display = "none";
	}
}

@foreach($users as $user)
<div class="button" onclick="open(document.getElementById("open"+{{ $user->id }}))">
       <h4>+</p> 
</div> 
<div id="open{{ $user->id }}">
	<h4>{{$user->name}}</p> 
</div>
@endforeach
kendrick's avatar

@bobbybouwmann - Thank you for the link

It works to open the the div:

<script type="application/javascript">
    var users = document.querySelectorAll('.open-user')
    users.forEach(function(user) {
      user.addEventListener('click', function() {
        var userId = this.getAttribute('user-id') 
        var y = document.getElementById('open-' + userId);
        document.getElementById('open-' + userId).style.display = 'block'  
      })
    })
  </script>
  <div user-id="{{$user->id}}" class="button open-user">
    <h4>+</p>  
  </div> 
  <div id="open-{{$user->id}}" style="display: none;">
    <h4>{{$user->name}}</p>
  </div>

As I add style="display: none;" manually I can't check if y is open or not - to close the div after another click. Is there a workaround?

kendrick's avatar

@bugsysha - Thank you - also tried it, but after I click on the button the screen turns white without any error in the console. What could that be?

bobbybouwmann's avatar

@splendidkeen The reason your approach before didn't work is that each item has the same id in HTML. An id needs to be unique in your HTML.

kendrick's avatar

@bobbybouwmann - Yes, I knew that was the problem, but couldn't find a workaround. Actually stepped over your link before opening the question - but it was a bit unclear then.

bugsysha's avatar

Probably CSS is playing tricks on you. Or have you fixed quotes?

kendrick's avatar

@bugsysha - Fixed quotes, as the button crashed when using only "", I put it this way: onclick="open(document.getElementById('open'+{{ $user->id }}))"

bugsysha's avatar

Why don't you give AlpineJS a try?

<script src="https://cdn.jsdelivr.net/gh/alpinejs/[email protected]/dist/alpine.min.js" defer></script>

@foreach($users as $user)
<div x-data="{visible: false}">
<button class="button" @click="visible = !visible">
       +
</button> 
<div x-if="visible"><!-- or x-show -->
	<h4>{{$user->name}}</p> 
</div>
</div>
@endforeach
bugsysha's avatar

No man, you just can't beat him. If you can't beat him, join him is what Bugs Bunny always said :D

Please or to participate in this conversation.