Go Back   CodingForums.com > :: Client side development > JavaScript programming > JavaScript frameworks

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-24-2009, 02:26 AM   PM User | #1
lauthiamkok
Regular Coder

 
Join Date: Dec 2008
Posts: 117
Thanks: 14
Thanked 0 Times in 0 Posts
lauthiamkok is an unknown quantity at this point
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
lauthiamkok is offline   Reply With Quote
Old 06-24-2009, 10:21 AM   PM User | #2
Iszak
Regular Coder

 
Iszak's Avatar
 
Join Date: Jun 2007
Location: Perth, Western Australia
Posts: 332
Thanks: 2
Thanked 58 Times in 57 Posts
Iszak is an unknown quantity at this point
Why not do this..
Code:
$(function(){
  $('.sub').click(function(){
    $('.sub').removeClass('current');
    $(this).addClass('current');
  });
});
Iszak is offline   Reply With Quote
Old 06-24-2009, 12:09 PM   PM User | #3
lauthiamkok
Regular Coder

 
Join Date: Dec 2008
Posts: 117
Thanks: 14
Thanked 0 Times in 0 Posts
lauthiamkok is an unknown quantity at this point
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!
lauthiamkok is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:25 AM.


Advertisement
Log in to turn off these ads.