Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Dec 2008
    Posts
    117
    Thanks
    14
    Thanked 0 Times in 0 Posts

    jquery: adding a function to all links on page read

    Hi,
    How can i add a function to all links which share a same class on page read? I want to add this addClassSub(); to all the links with a class name 'sub'.

    this is the html,

    PHP Code:
    <li class="sub"><a href="#">1</a></li>
    <
    li class="sub"><a href="#">2</a></li>
    <
    li class="sub"><a href="#">3</a></li
    this is the jquery and functions,

    Code:
    $(document).ready(function(){
    $(".sub").click(function () {
    	addClassSub();
    	return false;
        });
    });
    
    function addClassSub() {
    	$(".sub").click(function () {
    	removeClassSub();
    	$(this).addClass("current");
        });
    	}
    
    function removeClassSub() {
    	$(".sub").removeClass("current");
    	}
    many thanks,
    Lau

  • #2
    Regular Coder Iszak's Avatar
    Join Date
    Jun 2007
    Location
    Perth, Western Australia
    Posts
    332
    Thanks
    2
    Thanked 58 Times in 57 Posts
    Why not do this..
    Code:
    $(function(){
      $('.sub').click(function(){
        $('.sub').removeClass('current');
        $(this).addClass('current');
      });
    });

  • #3
    Regular Coder
    Join Date
    Dec 2008
    Posts
    117
    Thanks
    14
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Iszak View Post
    Why not do this..
    Code:
    $(function(){
      $('.sub').click(function(){
        $('.sub').removeClass('current');
        $(this).addClass('current');
      });
    });
    it works perfectly! thanks!


  •  

    Posting Permissions

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