I would like some help to optimize my code.
I have a product list.
<ul>
<li><a href="#" id="item1"> Item 1 </a></li>
<li><a href="#" id="item2"> Item 2 </a></li>
</ul>
For every item I have a section with the item's details.
<div id="item1-details"> Item Details 1 </div>
<div id="item2-details"> Item Details 2 </div>
These sections are hidden. They only appear when the user clicks on the item listed above.
$("#item1").click(function() { $("#item1-details").show(); });
$("#item2").click(function() { $("#item2-details").show(); });
Up to this point I have no problems. The logic is very basic. The problem comes when the information is retrieved from the database.
When the user clicks on an item, how do I pass the ID of the related detail's section to the jQuery function?
I have two foreach blocks. One to populate the - ul - list and another to create the detail's sections. I can use the item ID to make unique the link and use the same ID adding a character to make unique the details section. But how can I create this automatism with jQuery.
I hope I was clear, even with my English. Thank you all.
onclick="$('#' + this.id + '-details').show();"