Google Groups Home
Help | Sign in
add/remove class click function
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  7 messages - Collapse all
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
thekman  
View profile
 More options May 11, 3:20 pm
From: thekman <dai...@propertyireland.net>
Date: Sun, 11 May 2008 12:20:42 -0700 (PDT)
Local: Sun, May 11 2008 3:20 pm
Subject: add/remove class click function
hi all,
i am using the code below to hide some content, the first part works
ok & hides the content as expected but i cant get the second part to
work - i.e. showing the content again - any ideas?
i don't want to use toggle as i am displaying the content based on a
cookie value & when the cookie is set, it takes 2 clicks to display
the content if it was hidden by the cookie.

$('.min').click(function() {
$
(this).removeClass("min").addClass("max").parents(".Container").children(". Content").hide();

});

$('.max').click(function() {
$
(this).removeClass("max").addClass("min").parents(".Container").children(". Content").show();


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Dave Methvin  
View profile
 More options May 11, 3:55 pm
From: Dave Methvin <dave.meth...@gmail.com>
Date: Sun, 11 May 2008 12:55:41 -0700 (PDT)
Local: Sun, May 11 2008 3:55 pm
Subject: Re: add/remove class click function

> i am using the code below to hide some content, the first part works
> ok & hides the content as expected but i cant get the second part to
> work - i.e. showing the content again - any ideas?

Can you show a sample of the markup, or better yet link to a test page?

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
thekman  
View profile
 More options May 11, 4:33 pm
From: thekman <dai...@propertyireland.net>
Date: Sun, 11 May 2008 13:33:13 -0700 (PDT)
Local: Sun, May 11 2008 4:33 pm
Subject: Re: add/remove class click function
Hi Dave,
Below is the code...

<script language="javascript" type="text/javascript">
$('.min').click(function() {
$
(this).removeClass("min").addClass("max").parents(".Container").children(". Content").hide();
$.cookie($(this).parents(".Container").attr("id"), 'closed',
{ expires: 7 });

});

$('.max').click(function() {
$
(this).removeClass("max").addClass("min").parents(".Container").children(". Content").show();
$.cookie($(this).parents(".Container").attr("id"), 'closed',
{ expires: -1 });
});

</script>

<div class="Container" id="reviews">
<h3><div class="ctrls"><a href="#"><span class="min"></span></a></
div>Reviews</h3>
<div id="reviewsContent" class="Content">content text...</div>
</div>

On May 11, 8:55 pm, Dave Methvin <dave.meth...@gmail.com> wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Dave Methvin  
View profile
 More options May 11, 8:45 pm
From: Dave Methvin <dave.meth...@gmail.com>
Date: Sun, 11 May 2008 17:45:17 -0700 (PDT)
Local: Sun, May 11 2008 8:45 pm
Subject: Re: add/remove class click function
Ah, I think I see it, but I should have seen it earlier.

I'm going to assume your two .click() methods are inside a .ready()
handler or other block that doesn't execute until the markup is there.

Remember that the .click() method sets an event handler for the
elements matching the selector **at the time the .click() method is
called**.

So your handler for ".min" attaches to the elements that initially
have a class of min. There aren't any elements with the class max so
that one does nothing. That explains why the min works but the max
doesn't.

It looks like you're using the min and max classes to show some sort
of image defined by css, so you probably want to keep those the way
they are. If all your entries are initially at min, you could just
attach a single handler with the ".min" and handle the toggling inside
that one handler. If some start at min and some at max, that wouldn't
work.

What I would do is create another class, say minmax, and attach the
event handler with that. Inside the handler, see if the current class
is min or max and do the opposite.

$('.minmax').click(function() {
  var $this = $(this);
  if ( $this.is(".max") ) {
    $this.removeClass("max").addClass("min")
       .parents(".Container").children(".Content").show();
    $.cookie($this.parents(".Container").attr("id"),
        'closed', { expires: -1 });
  } else {
    $this..removeClass("min").addClass("max")
       .parents(".Container").children(".Content").hide();
    $.cookie($this.parents(".Container").attr("id"),
       'closed', { expires: 7 });
  }

});

Or a shorter version:

$('.minmax').click(function() {
  var $this = $(this);
  var togv = $this.is(".max")?
     [ "max", "min", "show", -1 ] :
     [ "min", "max", "hide", 7 ];
  $this.removeClass(togv[0]).addClass(togv[1]).
       .parents(".Container").children(".Content")[togv[2]]();
   $.cookie($this.parents(".Container").attr("id"),
        'closed', { expires: togv[3] });

});

<span class="min minmax"></span>

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
thekman  
View profile
 More options May 11, 10:04 pm
From: thekman <dai...@propertyireland.net>
Date: Sun, 11 May 2008 19:04:57 -0700 (PDT)
Local: Sun, May 11 2008 10:04 pm
Subject: Re: add/remove class click function
Thanks a million Dave,
both examples work a treat...
I just had to remove the extra . after $this in (example 1)

and in example 2, remove the extra . between addClass(togv[1]).
       .parents(

and change my layout code to <span class="min minmax"> rather than
<span class="min">
it now works as expected...

so thanks again...


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
thekman  
View profile
 More options May 12, 10:19 am
From: thekman <dai...@propertyireland.net>
Date: Mon, 12 May 2008 07:19:03 -0700 (PDT)
Local: Mon, May 12 2008 10:19 am
Subject: Re: add/remove class click function
Hi & thanks again for your help Dave.
Do you know of a better way to do this bit?

<h3><div class="ctrls"><a href="#"><span class="min"></span></a></
div>Reviews</h3>

i am using the href as i want to use a hover image, but if it is far
down the page & a user clicks on it to show/hide the content, the
browser scrolls back to the top of the page.
any ideas?

On May 12, 1:45 am, Dave Methvin <dave.meth...@gmail.com> wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
thekman  
View profile
 More options May 12, 11:08 am
From: thekman <dai...@propertyireland.net>
Date: Mon, 12 May 2008 08:08:09 -0700 (PDT)
Local: Mon, May 12 2008 11:08 am
Subject: Re: add/remove class click function
fixed it, just had to add return false; to the click function.

On May 12, 3:19 pm, thekman <dai...@propertyireland.net> wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2008 Google