1

In the following html, jquery, bootstrap code,

addMenuBar : function addNavigation (component) {
    var html = '<ul class="nav nav-pills nav-justified" role="tablist">';
    html += '<li id ="scenario" class="disabled"><a href="#">Scenario</a></li>';
    html += '<li id ="resolutions" class="disabled"><a href="#">Resolutions</a></li>';
    html += '<li id ="triggers" class="disabled"><a href="#">Triggers</a></li>';
    html += '</ul>';
    component.append(html);
    $('ul[id="scenario"] li').addClass("active");
}

where the component is a container div as follows:

    this.container = $(document.createElement('div'));
    this.container.addClass('patient-hover-inner');
    this.container.appendTo(this.form);

I want to change the class for the 'li' item for id = 'scenario' from 'disabled' to 'active'. how do i do this? The above won't work without any errors

2
  • $(document.createElement('div')) You are using jQuery: $("<div>") Commented May 31, 2015 at 17:27
  • Firstly you're selecting a UL with a class of scenario whereas you want the LI. Secondly [id="scenario"] should just be #scenario, and thirdly when dealing with IDs they should be unique, so you just want $('#scenario') - no tagnames necessary Commented May 31, 2015 at 17:32

2 Answers 2

2

Use switchClass()

$('li.disabled').switchClass("disabled", "active");

If you are not using jQuery UI, you can do

$('li.disabled').each(function(){
    $(this).removeClass('disabled');
    $(this).addClass('active');
});
Sign up to request clarification or add additional context in comments.

7 Comments

Only available in jQuery UI.
that wont work either, i have created a fiddle in my question
@Rookie, where is the fiddle?
@Rookie, that link is broken
its working now, just that the javascript is in the html so its throwing messages
|
1
$('ul[id="scenario"] li').addClass("active");

can be

$('ul li#scenario').removeClass("disabled").addClass("active");

If a li has the disabled class it will be removed. If it doesn't then nothing happens. In either case the 'active' class will be added.

9 Comments

The ul you are adding before that selector doesn't have an id=scenario. Your selector should actually be $('ul li#scenario')
My intent is to activate just one 'li' which has the id=scenario from the 'ul' list. how do i do that?
you might have to add the bootsrap CSS and jquery as an external link to the fiddle from this link: bootstrapcdn.com
It is because your component is not yet in the DOM when you try to select anything inside of it. You will have to wait till after your component has been added to the DOM itself before you can select any items that are being appended to the component in your functions.
Finally got your fiddle to work an example of what I just mentioned above: jsfiddle.net/r3g62zuz/1
|

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.