0

I am new to jQuery. I want to apply css class to my <li> element but it's not working. Can anyone help me?

My markup is:

<ul class='change'>
    <li>companyName</li>
    <ul id='uid'>
        <li onClick='callTest()'>subcompany</li>
        <li onClick='callTest()'>subcompany</li>
        <li onClick='callTest()'>subcompany</li>
    </ul>

  <ul id='uid'>
        <li onClick='callTest()'>subcompany2</li>
        <li onClick='callTest()'>subcompany2</li>
        <li onClick='callTest()'>subcompany2</li>
    </ul>
    </li>
</ul>

My problem is when I call the callTest() method, I want to change the background color for that <li> i am trying this way not working:

$('#uid li').click(function () {
    console.log("call lil...'");
    $(this).addClass('selected').siblings().removeClass('selected');
})

It's working fine but the first I selected subcompany it's applied selected class for that and now I selected subcompany2 for this also applied selected class but on this time I want to remove subcompany class. How can I do this?

2
  • Works fine here jsfiddle.net/ggNrr are you getting any errors? Commented Aug 28, 2013 at 8:00
  • You don't need the onclick="callTest()" if you are using jQuery click handlers Commented Aug 28, 2013 at 8:00

9 Answers 9

1

Modify your HTML this way:

<ul class='change'>
    <li>companyName</li>
    <ul id='uid'>
        <li>subcompany</li>
        <li>subcompany</li>
        <li>subcompany</li>
    </ul>
</ul>

and ADD this JS this way:

$('#uid > li').each(function () {
    $(this).on("click", function() {
        console.log("call lil...'");
        $(this).addClass('selected').siblings().removeClass('selected');
    });
});
Sign up to request clarification or add additional context in comments.

Comments

1

The problem looks like you are targeting all li elements, including the parent instead of targeting only those are under uid

$('#uid li').click(function () {
    console.log("call lil...'");
    $(this).addClass('selected').siblings().removeClass('selected');
})

Demo: Fiddle

Comments

0

It is working fine actually

$('ul li').click(function(){
    console.log("call lil...'");
    $(this).addClass('selected').siblings().removeClass('selected');
});

Here is the demo: JsFiddle

Check your html file. There must be a problem including your script or the jquery.

Comments

0

You code is right. Since you've added this script in head/body it is reason for not working.

So just Wrap it in $(document).ready(function()..

$(document).ready(function(){
$('ul li').click(function(){
    console.log("call lil...'");
      $(this).addClass('selected').siblings().removeClass('selected');
    })
});

Also try to used .on() event handler like this

$(document).ready(function(){
$('ul li').on('click', function(){
    console.log("call lil...'");
      $(this).addClass('selected').siblings().removeClass('selected');
    })
});

Comments

0

Remove the onclick at the li and add handlers when document is ready:

$(document).ready(function(){
  $('ul li').click(function () {
    console.log("call lil...'");
    $(this).addClass('selected').siblings().removeClass('selected');
    callTest();
  })
});

Comments

0

DEMO

$('li').click(function (e) {
    $('li').removeClass('selected');
    $(e.target).addClass('selected');
})

Comments

0

Call callTest() inside che click handler.

Comments

0

onclick attribute is not needed in the li elements. Remove those

Comments

0

There is a HTML-issues here:

<ul class='change'>
    <li>companyName
        <ul id='uid'>
            <li onClick='callTest()'>subcompany</li>
            <li onClick='callTest()'>subcompany</li>
            <li onClick='callTest()'>subcompany</li>
        </ul>
    </li> <!-- I moved this one -->
</ul>

regarding your jQuery you can do it like this instead:

$('ul.change > li').click(function(){ //Only applies to the direct child of ul.change
    console.log("call lil...'");
      $(this).addClass('selected');
    })

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.