I have a jquery function that toggles a hide and show for a p tag. in my HTML, it brings multiple different store.IDs in the results. There are specific p tags in the search for each store ID and I just want it to show/hide the specific one the user clicks. When I run this, all the p tags for all the stores showing toggle hide and show, not the specific one the user clicks. Is there a way to make it specific to the users click?
HTML:
<div>
<a class="store-detail-link" href="${URLUtils.url('Stores-Details', 'storeID', store.ID)}">
Details
</a>
<p class="${store.ID} test"></p>
</div>
JS:
$('.test').toggle();
The user will get multiple store IDs results. They should be able to toggle the "Details" button on the specific store ID they click, not all the "Details" that get shown.
2
Answers
The
id
of each element is irrelevant here. ID’s should be avoided whenever possible because their use creates brittle code that doesn’t scale well. Instead, get referenced to elements in a more relative way. That is, find your elements based on where in the DOM structure something is located.See comments inline.
To make the toggle functionality specific to the user’s click on the store-detail-link element, you need to modify your jQuery code. Here’s an updated version that should achieve the desired behavior: