Welcome Guest!

If you do not have an account yet on The Web Squeeze forums, please Register! It’s FREE and there are many benefits:

  • Receive Fast Advice
  • Learn Programming Languages
  • Get Professional Website Reviews
  • Quick Troubleshooting Assistance

> A Flaw In My Jquery Logic I Think

This is a discussion on A Flaw In My Jquery Logic I Think, within the Javascript section. This forum and the thread "A Flaw In My Jquery Logic I Think" are both part of the Frameworks category.

 
Reply to this topicStart new topic
> A Flaw In My Jquery Logic I Think, can u see the flaw in my method?
cosmicbdog
post Nov 16 2008, 11:33 PM
Post #1


Rapid Squeezer
****

Posts: 146
Joined: 3-July 08


Basically, say u have 10 divs on screen. You click one, and the other 9 dissapear.

What I'm doing to achieve this is I give those divs a class 'blah' and a unique id. I hide all divs with a class 'blah' and then that hides all 10. Then I show the unique Id. Backwards then forwards.

How though to just hide all the other classes named 'blah' except this one?
Go to the top of the page
 
+Quote Post
Rakuli
post Nov 16 2008, 11:37 PM
Post #2


Squeeze Machine
Group Icon

Posts: 766
Joined: 13-February 08
From: Catching the squeezed drips downunder.


CODE
$('.blah').click(function()
{

    $('.blah').hide();
    $(this).show();
});



Just reference the clicked instance specifically using the 'this' variable. Then you can do what you want without having to give anything an id at all.

Or you could quickly remove and the add the blah class

CODE
$('.blah').click(function ()
{
     $(this).removeClass('blah');
     $('.blah').hide();
     $(this).addClass('blah');
});


--------------------
Luke Dingle . com

Turn Over a Playful Leaf on Web Design -- read about the javascript cat
Go to the top of the page
 
+Quote Post
cosmicbdog
post Nov 17 2008, 12:30 AM
Post #3


Rapid Squeezer
****

Posts: 146
Joined: 3-July 08


I see. 'this' is a neat little one.

But the theory is still the same right, hide the others, show this one. There is no 'apply this to everything named this other than that'? This method is basically the fast form of doing it AND is good practice?
Go to the top of the page
 
+Quote Post
Rakuli
post Nov 17 2008, 01:05 AM
Post #4


Squeeze Machine
Group Icon

Posts: 766
Joined: 13-February 08
From: Catching the squeezed drips downunder.


Yeah, a div isn't really self-aware apart from the fact that "I was clicked" and "this points to me" aspect.

This would be the fastest method because the other option ($('.blah').each(function()) performing a check on each matched class is a lot slower (executing as many functions as there are matched divs).


--------------------
Luke Dingle . com

Turn Over a Playful Leaf on Web Design -- read about the javascript cat
Go to the top of the page
 
+Quote Post
cosmicbdog
post Nov 17 2008, 03:33 AM
Post #5


Rapid Squeezer
****

Posts: 146
Joined: 3-July 08


Thanks for clearing that up Rakuli.
Go to the top of the page
 
+Quote Post
If you found The Web Squeeze to be helpful, please donate so we can keep this site FREE, FRESH, and fortified with Web Design & Development info!
Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Collapse

> Similar Topics

    Topic Title Replies Topic Starter Views Last Action
No New Posts   2 Jason 382 6th May 2008 - 07:33 AM
Last post by: Rakuli
No New Posts   8 unitedcraig 575 9th September 2008 - 10:03 PM
Last post by: Monie
No New Posts   10 MikeHopley 1,273 21st June 2008 - 07:40 PM
Last post by: karinne
No new   19 jackfranklin 609 21st June 2008 - 07:39 PM
Last post by: karinne
No New Posts   3 Monie 319 29th June 2008 - 05:46 PM
Last post by: sylvatica