Reza Paidar
Reza Paidar

Reputation: 883

add active class to current hyperlink using asp.net c#

I'm trying to show which link is current active using j query in aspx web form.
first I generated all hyperlinks from my db to gallery.aspx by this function from behind code:

public string build_Gallery_Category()
{
    string post = "";
    DataTable dt = new DataTable();
    clsBusiness cls = new clsBusiness();
    dt = cls.Fill_In_DataTable("GalleryGroup");
    if (dt.Rows.Count > 0)
    {
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            post += "<li><a href='gallery.aspx?gid=" + dt.Rows[i]["GalleryId"].ToString() + "'><strong>" + dt.Rows[i]["GalleryName"] + "</strong></a></li>";
        }
    }
    return post;
}

So, after i call that method, all of hyper links appear on gallery.aspx, as bellow :

<div id="menu">
    <ul>
       <%=viewCategory %>
    </ul>
</div>

after that, add this style to gallery.aspx:

<style>
    .active { background: #f00; }
</style>

after all, I added jquery code on end of from :

<script>
    $("menu li").click(function (e) {
        e.preventDefault();
        $("menu li a.active").removeClass("active"); //Remove any "active" class  
        $("a", this).addClass("active"); //Add "active" class to selected tab  
    });
</script>

but my code does't work... so please help me. thanks alot...

Upvotes: 0

Views: 1177

Answers (1)

Xaver
Xaver

Reputation: 393

looks like your jQuery selector is wrong

$("menu li")

must be

$("#menu li")

and

$("menu li a.active")

must be

$("#menu li a.active")

without the # you cannot access the div-tag with id menu

Upvotes: 1

Related Questions