frenchbaguette
frenchbaguette

Reputation: 1339

JAVASCRIPT pop up box display

This is my html:

<div class="popup" onclick="itemPopup()">

    <img src="{{$item->item->img}}">
    <span class="popuptext" id="myPopup">

    {{$item->title}}<br> 
    <a href="/sell-item/{{$item->id}}" class="btn-sm btn-danger" style="text-decoration: none">
    Sell for {{$item->price/2}} <img src="/img/gold.png"></a><br>
    <p>{{$item->description}}</p>

    </span> 
</div>  
@endforeach

This is my js:

function itemPopup() {
    var popup = document.getElementById("myPopup");
    popup.classList.toggle("show");
}

When I click on image the pop up shows like this:

enter image description here

No matter if I click on second sword, the pop up box will still appear on first sword with his id. How to fix this issue ?

Upvotes: 0

Views: 88

Answers (1)

Claudio Pinto
Claudio Pinto

Reputation: 389

Assign unique ids to each iteration of your blade and then pass that same ID in the JavaScript function call. Your HTML will look like this.

<div class="popup" onclick="itemPopup('myPopup{{$item->id}}')">

    <img src="{{$item->item->img}}">
    <span class="popuptext" id="myPopup{{$item->id}}">

    {{$item->title}}<br> 
    <a href="/sell-item/{{$item->id}}" class="btn-sm btn-danger" style="text-decoration: none">
    Sell for {{$item->price/2}} <img src="/img/gold.png"></a><br>
    <p>{{$item->description}}</p>

    </span> 
</div>  

And the JavaScript:

function itemPopup(ID) {
    var popup = document.getElementById(ID);
     popup.classList.toggle("show");
}

Upvotes: 1

Related Questions