tags:

views:

58

answers:

6

Hi guys,

I have the following structure :

<ul id='myTopicsList'>

<li> 
<a><span> First Element </span></a>
</li>

.....

</ul>

The first time the page is loaded the first li will be selected by highliting it to background color blue.

The next time the user clicks another element in the list it should change to blue and the rest should have white background.

I am using this script :

function GetMyTopic(catID) {
 $('#myTopicsList li').each(function () {

        if ($(this).attr('id').indexOf(catID) > 0) {
            $(this).addClass('SideBarBoxliSelected');
        }
        else {
            $(this).addClass('SideBarBoxli');
        }
    });
}

here the css :

.SideBarBoxli{margin-bottom:4px; background-color:#fafafa; height:22px; }
.SideBarBoxli:hover {background-color:#E3ECF8; cursor:pointer; }
.SideBarBoxliSelected{margin-bottom:4px; background-color:#6388BF; height:22px; }

When I click and assign the SideBarBoxliSelected class to the cliked li the background remains the same.

Any suggestions?

+3  A: 

Here is the html I would use:

<ul id="myTopicsList">
    <li class="SideBarBoxli SideBarBoxliSelected">first</li>
    <li class="SideBarBoxli">sec</li>
    <li class="SideBarBoxli">three</li>
    <li class="SideBarBoxli">four</li>
    <li class="SideBarBoxli">fiffff</li>
</ul>

Here is the code I would use:

$(document).ready(function() {
    $('ul#myTopicsList li').click(function() {
        $('li.SideBarBoxliSelected').removeClass('SideBarBoxliSelected');
        $(this).addClass('SideBarBoxliSelected');
    });
});

The .click function will set it so that whenever someone clicks on any of the li items they will trigger the anonymous function which removes the class SideBarBoxliSelected from the currently selected one and adds it to the clicked item.

Edit


You can also add the following to make sure the first item has the class SideBarBoxliSelected on page load:

$('ul#myTopicsList li:first').addClass('SideBarBoxliSelected');

The above line would go on the line before the $('ul#myTopicsList li').click call.


samandmoore
ul#myTopicList should be ul#myTopicsList fyi
ryanulit
I have applied the code u've posted. but when the page loads for the first time, the first li has its background #fafafa and not blue(#E3ECF8).I have on each li element an onclick event calls GetMyTopic and passes the catID to do other work.hmmm...
Joseph Ghassan
ok, clicking on each item works correctly. remains when the page is loaded first time, the first element is not selected
Joseph Ghassan
Any idea why when the page is first loaded the first element isn't highlighted blue?
Joseph Ghassan
@Joseph Ghassan, can you please share with us your actual code so that we can try to better help you resolve this issue?
samandmoore
A: 

When is GetMyTopic called? I would suggest using the Firebug plugin for Firefox to debug your javascript. Make sure the right CSS class is being applied to each LI involved in your test case. If not, you can figure out why by stepping through the script in the debugger. You can also check out the CSS in firebug to see what styles are getting applied.

Also, in your sample code you are using the 'id' attribute to identify list items, but you don't have an id attribute defined in your HTML or script. Maybe something is getting left out?

RMorrisey
GetMyTopic is called when an li element is clicked. I have assigned it to the onclick event of li.
Joseph Ghassan
A: 

I'm not sure using the indexOf function is the best way to compare the id to your passed in variable. Maybe try using:

if ($(this).attr('id') == catID)

instead?

Jeremy
The reason why I am using indexOf is that each li has the following id pattern : myCat{0} where {0} is the placeholder of id.
Joseph Ghassan
Ah, i see - so the actual ID is part of a bigger string. In any case, samandmoore's suggestion above is a better solution. I would go with what he suggests.
Jeremy
+1  A: 
Rahul
Perfect! it is working here. one thing remains though. When the page is first loaded the first element is not selected though its class is class="SideBarBoxli SideBarBoxliSelected".
Joseph Ghassan
A: 

I think you need to add the period in front of "SideBarBoxliSelected" so .SideBarBoxliSelected

Billy
A: 

This is the simplest way to do it.

HTML

<ul id="myTopicsList">
    <li>first</li>
    <li>second</li>
    <li>third</li>
    <li>fourth</li>
</ul>

CSS

#myTopicsList li {background:#fafafa;margin-bottom:4px;height:22px;}
#myTopicsList li:hover {background:#E3ECF8;cursor:pointer;}
#myTopicsList li.active {background:#6388BF;}

jQuery

$(document).ready(function(){
    $('#myTopicsList li').click(function(e){
        // remove all active classes
        $('#myTopicsList li').removeClass('active');
        // add active class to clicked item
        $(this).addClass('active');
    });
    // click on the first item on page load
    $('#myTopicsList li').eq(0).click();
});
washwithcare