0

I am looking for a solution to do the following:

I have two links in a list:

<ul>
     <li class="1"><a>Option #1</a><li>
     <li class="2"><a>Option #2</a><li>
</ul>

When I click on a link, I want class to change to "selected", for example, I clicked "Option #1":

<ul>
     <li class="1 selected"><a>Option #1</a><li>
     <li class="2"><a>Option #2</a><li>
</ul>

The solution in jQuery is not necessary, but I guess jQuery is the right tool to do this?

Kindly help.

1
  • What should happen when you click "Option #2"? Commented Jan 7, 2015 at 1:45

2 Answers 2

2

You can do:

$('ul li').click(function() {
    $(this).addClass('selected').siblings('li').removeClass('selected');
});

You also need to close <li> using </li> instead of <li> to make your HTML markup valid.

Fiddle Demo

Sign up to request clarification or add additional context in comments.

Comments

0

You can use the jQuery addClass and removeClass functions, or if you want to save you some time the toggleClass

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.