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

Yablochnyi's avatar

How to pass a product to a modal window?

Good afternoon! Tell me how to transfer the selected product from the cycle to the modal window?

$('.js-modal').on('click', function(e) {
		e.preventDefault();
		var id = $(this).data('modal');
		$.fancybox.open({
			src: '#' + id,
			type: 'inline',
		})
	});
@foreach($category->products as $product)
                        <div class="catalog__item js-modal" data-modal="modal-product">
                            <div class="catalog__item-image">
                                <img src="{{'storage/' . $product->image}}" alt="pizza">
                            </div>
                            <h4 class="catalog__item-name">{{$product->name}}</h4>
                            <p class="catalog__item-desc">{{$product->description}}</p>
                            <div class="catalog__item-bottom">
                                <p class="catalog__item-cost">{{$product->price}} ₽ </p>
                                <button class="btn">Выбрать</button>
                                <button class="plus">
                                    <svg>
                                        <use xlink:href="{{asset('assets/images/sprite.svg#add')}}"></use>
                                    </svg>
                                </button>
                            </div>
                        </div>
                    @endforeach
0 likes
2 replies
jlrdw's avatar

I don't understand:

src: '#' + id,

What is the route to get the data.

But normally I just have an addEventListener which has display block to show the modal.

Tray2's avatar

My guess is that this src: '#' + id, needs to exist to be able to be used as the source, and I don't see anything setting the id of an element.

Be aware that you can have numeric ids on your elements, you should prefix them.

Please or to participate in this conversation.