homepage Welcome to WebmasterWorld Guest from 54.146.175.204
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
jQuery Toggle
ChrisBolton

5+ Year Member



 
Msg#: 4538096 posted 10:26 pm on Jan 21, 2013 (gmt 0)

This should be an easy one, but I've been struggling with it for over two hours now. I would greatly appreciate any help with this.

Iíve got a number of identical buttons on a page, each with a number of identical classes. For example:

<a href="#" class="a b c"><span class="y">button</span></a>
<a href="#" class="a b c"><span class="y">button</span></a>
<a href="#" class="a b c"><span class="y">button</span></a>

I want to be able to swap the class 'c' for a new class 'e' when I click the anchor, but only on the button I click. I also want the class on the span element to toggle between the current class of 'y' and a new class of 'z' with the same click.

Iím sure I have done this before but itís got me pulling my hair out right now. Can anyone help me please?

Thanks in advance.

 

ChrisBolton

5+ Year Member



 
Msg#: 4538096 posted 1:46 pm on Jan 22, 2013 (gmt 0)

I got it to work in the end by using:

(function() {
$('a.a').click(function() {
$(this).toggleClass("c").toggleClass("e").find("span").toggleClass("y").toggleClass("z");
});
})();

Can this be improved?

daveVk

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 4538096 posted 3:08 am on Jan 23, 2013 (gmt 0)

Try

$(this).toggleClass("c e").find("span").toggleClass("y z");

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved