I don't think you understand my question.

 

I have a link called 'Change the cats visibility'. I have a div with a cat
image inside it. I would like that particular link to change the visibility
of that particular image inside a div.

 

I want to be able to give the link an ID and the routine to toggle the
visibility a function in jQuery. 

 

From: jquery-en@googlegroups.com [mailto:[EMAIL PROTECTED] On
Behalf Of Aaron Heimlich
Sent: Tuesday, July 24, 2007 10:40 AM
To: jquery-en@googlegroups.com
Subject: [jQuery] Re: Toggling an objects visiblty without show and hide

 

 

On 7/24/07, Mitchell Waite <[EMAIL PROTECTED]
<mailto:[EMAIL PROTECTED]> > wrote: 

What does "hit" mean 

A jQuery object is an pseudo-array[1] that contains all of the HTML elements
selected by a give selector. So

$("div.foo");

is a pseudo-array of all of the "div" elements on the page that have the
class "foo". By "hit", I mean that toggleVis() will toggle each and every
one of the elements selected whatever selector you use (that's usually what
you want). 

and what is the "chain"?


 See
http://docs.jquery.com/How_jQuery_Works#Chainability_.28The_Magic_of_jQuery.
29

 

I don't want "everything to become invisible, just the one container I
specify.


That's exactly what my plugin does. Only the element's selected by the
selector that you use will be affected.

 

Wont th is work just fine?

 

jQuery.fn.toggleVis = function() {


        if(this.style.visibility == 'hidden') {
            this.style.visibility = 'visible';
        } else {
            this.style.visibility = 'hidden';
        }   
};

No it won't. In that context, "this" refers to a jQuery object, which does
not have a "style" property. To iterate over the contents of a jQuery
object, use the "each" method[2], as plugin does. The "each" method accepts
a function, and within that function "this" is set to whatever element is
currently being iterated over. The "each" method returns the jQuery object
it was iterating over, which allows users to continue chaining method calls
onto each other. 

The concept of "this" is certainly one of the more confusion aspects of
jQuery (and of JavaScript in general). For now, I direct you to
http://docs.jquery.com/How_jQuery_Works#Callbacks.2C_Functions.2C_and_.27thi
s.27, although there was a post by Michael Geary some time ago[3] that
explained this concept very well (as he always does).

[1] It's not an actual instance of JavaScript's Array class, but it acts
enough like one that it can be used mostly in the same fashion. You can
iterate over it like an Array object, and you can access indexes (foo[0],
foo[1], foo[2], etc.) like you can with an Array object, but that's about
it. 

[2] http://docs.jquery.com/Core#each.28_fn_.29
[3] I believe this is it:
http://groups.google.com/group/jquery-en/browse_thread/thread/d670a83c308961
d3/92e29565dff28d32#92e29565dff28d32


-- 
Aaron Heimlich
Web Developer
[EMAIL PROTECTED]
http://aheimlich.freepgs.com 

Reply via email to