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 10-27-2009, 11:47 PM   PM User | #1
met
Regular Coder

 
Join Date: Oct 2009
Location: United Kingdom
Posts: 728
Thanks: 4
Thanked 119 Times in 119 Posts
met has a little shameless behaviour in the past
jquery toggle/hide problem

hi

can someone more enlightened than myself work out why this won't work?

Code:
    $("h2.portfolio-title").click(function() {
	$('#portfolio').toggle("slow");
	$(this).replaceWith("<h2 class='portfolio-title1' >" + $(this).text() + "</div>");
   });
   
    $("h2.portfolio-title1").click(function() {
	$('#portfolio').hide("slow");
	$(this).replaceWith("<h2 class='portfolio-title' >" + $(this).text() + "</div>");

   });
essentially. I click the H2 with a class of "portfolio-title", and the #portfolio DIV scrolls nicely in to place, and I replace the class "portfolio-title" with "portfolio-title1".

This is reflected in the source, so the first part is definitely working - I am able to toggle a DIV on a click and replace its class with another.

I would then like to be able to click the header again, and hide the DIV and replace the class so the user is able to show/hide a div at will.

Toggle on its own works fine, but I would like to apply a different class to the "shown" header so the user is aware that they have clicked it.

What's wrong?

thanks.

Last edited by met; 10-28-2009 at 12:28 AM..
met is offline   Reply With Quote
Old 10-28-2009, 12:13 AM   PM User | #2
Fumigator
UE Antagonizer


 
Fumigator's Avatar
 
Join Date: Dec 2005
Location: Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
Posts: 7,686
Thanks: 42
Thanked 637 Times in 625 Posts
Fumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of light
The problem is this code is all loaded and interpreted right as soon the page loads. Well, when the page loads, the selector $("h2.portfolio-title1") has no match! You haven't created it yet! Vexing.

Fortunately the latest version of jQuery has a function called live() that you can replace the bind() function with (that click() function you are using is just a shortcut for bind(click(function{})) ). This live() function performs some magic to re-evaluate itself whenever the DOM is modified (or something like that... don't really know exactly how jQuery does its magic).

http://docs.jquery.com/Events/live#typefn
__________________
Fumigator is offline   Reply With Quote
Users who have thanked Fumigator for this post:
met (10-28-2009)
Old 10-28-2009, 12:25 AM   PM User | #3
met
Regular Coder

 
Join Date: Oct 2009
Location: United Kingdom
Posts: 728
Thanks: 4
Thanked 119 Times in 119 Posts
met has a little shameless behaviour in the past
makes perfect sense, thank you!

and yes that worked perfectly, cheers.
met 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 05:37 AM.


Advertisement
Log in to turn off these ads.