SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Evangelist
    Join Date
    Dec 2008
    Location
    Plymouth, United Kingdon
    Posts
    449
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    jquery: to set the <a> link to return true again

    Hi,
    I have menu which nested with sub menu like this,

    Code:
    <div id="menu">
     <ul>
     <li><a href="#">menu1</a>
       <ul>
          <li>sub1</li>
          <li>sub2</li>
       </ul>
    </li>
    <li><a href="#">menu2</a></li>
    <li><a href="#">menu3</a></li>
    <li><a href="#">menu4</a></li>
    </ul>
    </div>


    and I just want the last item in the menu to return true when it is clicked. So my idea is to set all item to return false, then set the last item to return true again.

    Code:
    $('#menu > ul > li > a').click(function(){
    				return false;
    			});
    			
    $('#menu > ul > li > a:last').bind('click', function() {
    			  return true;
    			});
    but I think probably it is wrong bcos it wont work! many thanks if u have any ideas to do this.

    cheers,
    Lau

  2. #2
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,077
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    It's not the last <a> tag you should be looking for, but the last <li> tag, since there is only one <a> per <li> but there are multiple <li>'s.

    So you should do

    Code javascript:
    $('#menu > ul > li > a').click(function(){
    				return false;
    			});
     
    $('#menu > ul > li:last > a').bind('click', function() {
    			  return true;
    			});

    Or, more simple:
    Code javascript:
    $('#menu > ul > li:not(:last) > a').click(function(){
    				return false;
    			});

    (not tested)

  3. #3
    SitePoint Evangelist
    Join Date
    Dec 2008
    Location
    Plymouth, United Kingdon
    Posts
    449
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ScallioXTX View Post

    Or, more simple:
    Code javascript:
    $('#menu > ul > li:not(:last) > a').click(function(){
    				return false;
    			});

    (not tested)
    just tested it and it works great! thanks

    Quote Originally Posted by ScallioXTX View Post
    It's not the last <a> tag you should be looking for, but the last <li> tag, since there is only one <a> per <li> but there are multiple <li>'s.

    So you should do

    Code javascript:
    $('#menu > ul > li > a').click(function(){
    				return false;
    			});
     
    $('#menu > ul > li:last > a').bind('click', function() {
    			  return true;
    			});
    thank you for correcting this


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •