0

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...

2
  • build_Gallery_Category is on gallery.aspx? Commented Aug 12, 2014 at 17:42
  • no it's on seprate class and call it from gallery.aspx.cs Commented Aug 13, 2014 at 8:13

1 Answer 1

1

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

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

2 Comments

thanks you're right but now, the hyperlink href is disabled and does't send query to server.
I think this is because of the e.preventDefault(); If you click a link also the li tag is clicked and further action is prevented.

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.