0

I am trying to show/hide three divs based on jquery using <a href="#id">. But the code is not working. The code works fine when I map using <a> link using rel attribute.

For example: <a rel="cat1" class="selected">

Default.aspx

<div id="featuredleftdiv">
    <script type="text/javascript">
        var featuredposts = new ddtabcontent("featuredposts")
        featuredposts.setpersist(true)
        featuredposts.setselectedClassTarget("link")
        featuredposts.init(10000)
    </script>

    <ul id="featuredposts" class="featuredposts">
        <li><a href="#cat1" class="menu">a</a></li>
        <li><a href="#cat2" class="menu">b</a></li>
    </ul>

    <div class="clear"></div>

    <div id="cat1" class="featuredposts_content">
        <asp:UpdatePanel ID="UpdatePanel4" runat="server"> 
            <ContentTemplate>
                <asp:ListView ID="ListView4" runat="server" GroupItemCount="1" OnPagePropertiesChanging="ListView4_PagePropertiesChanging"></asp:ListView>
            </ContentTemplate>
        </asp:UpdatePanel>
    </div>  

    <div id="cat2" class="featuredposts_content">
        <asp:UpdatePanel ID="UpdatePanel5" runat="server"> 
            <ContentTemplate>
                <asp:ListView ID="ListView5" runat="server" GroupItemCount="1"  OnPagePropertiesChanging="ListView4_PagePropertiesChanging"></asp:ListView>
            </ContentTemplate>
        </asp:UpdatePanel>
    </div>  

JQuery

In Head section of html

<script type="text/javascript" src="Scripts/jquery-1.8.3.js"></script>
<script type="text/javascript">
    $("a.menu").click(function () {
        $("div.featuredposts_content").hide();
        $($(this).attr('href')).show();
        return false;
    });
</script>
4
  • 1
    what do you mean by "the code is not working", please try to explain the problem you are facing. Commented Dec 21, 2012 at 16:56
  • why $($(this).attr('href')).show(); ? Commented Dec 21, 2012 at 16:59
  • 1
    jsfiddle.net/mC23h - looks to be working for me. Commented Dec 21, 2012 at 17:00
  • 1
    @lante - it's to tell which div id to show from the href...basically it's saying $('#cat1').show(); Commented Dec 21, 2012 at 17:01

4 Answers 4

1

You're assigning event handlers before the elements exist on the page. Change the script in the head section to this...

<script type="text/javascript" src="Scripts/jquery-1.8.3.js"></script>
<script type="text/javascript">
    $(function() {
        $("a.menu").click(function () {
            $("div.featuredposts_content").hide();
            $($(this).attr('href')).show();
            return false;
        });
    });
</script>

The $(function() { }) code makes your script run when the document is ready, or when all the elements have been created.

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

1 Comment

But OP mentioned it works with "rel", so it should have same effect, i.e, not having events attached.
0

Try something like this...

​<html>
   <script>
      $(document).ready(function(){
         $("a.menu").click(function () {
            $("div.featuredposts_content").hide();
            $("#" + $(this).attr("value")).show();
         });​
      })
   </script>
   <body>
      <ul id="featuredposts" class="featuredposts">
         <li><a href="#" class="menu" value="IdOfDiv1">something a</a></li>
         <li><a href="#" class="menu" value="IdOfDiv2">something b</a></li>
      </ul>

      <div id="IdOfDiv1" class="featuredposts_content">
         Cat1
      </div>  

      <div id="IdOfDiv2" class="featuredposts_content">
         Cat2
      </div>
  </body>
</html>​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​

EDIT: Here's a quick explanation.

The $(document).ready() will make sure the wiring of the click does not happen until the page is fully loaded. When the page first loads, everything is being displayed.

Once a user clicks on one of the links with class of menu, the function will run. It will hide every div that have the class of featuredposts_content. Then, based on which link was clicked, it grabs the value and use that in setting which div to show. The value in the link is the id of the div to show.

1 Comment

Can you explain this answer?
0

Looks like the Update panels are messing it up.

You need to bind the event after every postback , otherwise it gets set back to the default page

$(function() {
    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(PostBack);
    PostBack();
});

function PostBack(){
       $("a.menu").off().on.('click' , function () {
            $("div.featuredposts_content").hide();
            $($(this).attr('href')).show();
            return false;
        });
}

Comments

0

Well i think issue is the not using the doc ready handler or load function for the events:

<script type="text/javascript">
   $(document).ready(function(){ // <-------------------------you are missing this
    $("a.menu").click(function () {
        $("div.featuredposts_content").hide();
        $($(this).attr('href')).show();
        return false;
    });
   }); //<----------------------------------------------------
</script>

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.