0

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.

2
  • 1
    clicks "know" what DOM element the click occured on. You can use regular dom operations to retrieve the ID of the item associated with the click, and work from there. e.g onclick="$('#' + this.id + '-details').show();" Commented May 30, 2014 at 16:19
  • 1
    Are you asking us to optimize something that works, or asking for help with something that doesn't work? It's not clear. If the latter, what doesn't work? What do you mean by "pass the ID"? When you render the hidden sections on the page, can't each one include its "id" from server-side code? What does any of that have to do with PHP or your database? Commented May 30, 2014 at 16:20

3 Answers 3

3

you can use data attributes like this

<li><a href="#" id="item1" data-id="1"> Item 1 </a></li>

and then

$("#item1").click(function() { 
    $attr = $(this).attr('data-id');
    $("#item"+$attr+"-details").show(); 
});
Sign up to request clarification or add additional context in comments.

2 Comments

+1 But you should change $("#item1") to a class so that it really can be re-used.
You can also use $(this).data('id') to get the value.
2

Based on the fact that the ids are related, you can just use simple click, grab the id, and show it.

$("li").on("click", "a", function(evt) {  //detect click on anchors in the li
    var id = this.id;                  //get id of what is clicked
    $("#" + id + "-details").show();   //show the element 
    evt.preventDefault();
});

Comments

1

You could do, get the id and with cointains (*=) show the the div:

$('div').hide();
$('ul a').click(function(){
    $('div[id*=' + this.id +']').toggle();
    })

DEMO

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.