0

I have a List that shows Products. When I click on one, I need to change the class of the <li> so as to show that it is clicked. I got this working... But what I can't do is clear the other <li> that have been previously clicked so as to show it is not selected anymore.

This is part of my code, what am I doing wrong? btw, my logic here is to first refresh all classes to notselected, and then just update the list with my product id number. this can be seen on the second line of code.

I'm new to javascript, and did my research, and this is what I got.

function showdetails(ProductID) {           
  document.getElementsByName("ProductList").className = "notselected";
  $("#"+ProductID).attr('class','selected');
1
  • thanks everyone. i'm giving the right answer to h2oooooo since he/she was the first to answer, and the code worked flawlessly. Commented Oct 28, 2013 at 18:38

2 Answers 2

3

Are you trying to do this?

function showdetails(ProductID) {    
    $("[name=ProductList].selected").removeClass('selected');
    $("#"+ProductID).addClass('selected');
}
Sign up to request clarification or add additional context in comments.

1 Comment

This is definitely better, no need to complicate things with a notselected class, when the absence of the selected class tells you the same information.
0

I think you need to iterate across all elements returned in document.getElementsByName("ProductList") and change className to each element individually.

var productLists = document.getElementsByName('ProductList');
for (i = 0; i < productLists.length; i++) {
  productLists[i].className = 'notselected'
}

2 Comments

@Xander That tripped me up at first too, but this is .getElementsByName(), not .getElementsByTagName(). This one queries on the name attribute.
I was assuming they had <something name="ProductList"></something> which would satisfy the query.

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.