views:

214

answers:

2

I'm trying to stop the browser from following certain links, rather I want to unhide some Divs when they are clicked.

I'm having trouble just getting the links to not be followed though.

Here's what I have:

var titles = $('a.highlight'); jquery.each(titles, function(){ this.click(function(){ return false; } ) } );

It seems like the click handler is not being assigned. What am I missing?

+3  A: 

Try

this.click(function(e){ e.preventDefault(); }

Actually, it looks like you might need to use the jQuery constructor on this:

$(this).click(function(){ return false; }

You could also try using parameters on the each function instead of using this:

jQuery.each( titles, function(index, elem) { $(elem).click( function() { return false; } ) } );

Personally, I would just do titles.each( ... though. In that instance you can use this to bind the click handler. I am not sure off the top of my head what this binds to with jQuery.each

Or just calling click on titles:

titles.click( function() { return false; } )

That will bind click to every element in titles. You don't need to loop through them.

Tinister
+1 for $(this) in the each() handler
spender
Yep, that's it. Just using `this` returns a dom object. Using $(this) returns a jquery object which is necessary for attaching a handler using the .click function.
Damovisa
By the way, I just looked it up and `this` in the `jQuery.each` function does refer to the object currently being iterated on.
Tinister
@Tinister - really? Do you have a link? (I do believe you, I just wanna see :))
Damovisa
Uh, http://docs.jquery.com/Utilities/jQuery.each -- I initially thought `this` might've been something different for some weird reason.
Tinister
@Tinister - I don't think that's correct. For example, try `$.each($('li'), function() { this.click(); });` and compare it to `$.each($('li'), function() { $(this).click(); });` One works and one doesn't: `.click` is not a function of `this`, but it is a function of `$(this)`.
Damovisa
Derp. That's not what I meant. I did not know what `this` for `jQuery.each` was defined as and didn't look it up in my rush to get an answer out. In contrast, I did know that `this` in `$().each` is defined as the dom element that's in the resultant jQuery collection. I knew that for `jQuery.each` (with iterates over anything, not just jQuery collections) you could get the object being iterated on as a parameter, but was unsure what `this` was. Make sense?
Tinister
@Tinister - I'm with you. `jQuery.each()` != `$().each`
Damovisa
A: 

You can compress that jquery a bit:

$('a.highlight').click(function() { return false; });

You should also make sure that:

  • There are no other click handlers registered for those elements later on.
  • The code you have is attaching after the elements have loaded. If they're not completely loaded, they won't be found in the $('a.highlight') selector. The easiest way to do this is to put your code in a $(document).ready(function() { *** code here *** }); block.

Edit: As per other responses - the problem was that this represents a DOM object, while $(this) is a jquery object. To use the .click function to attach a handler, you need a jquery object.

In short, using this inside the each loop won't work with what you're trying to do. You'll need to get a jquery representation by using $(this) instead.

Damovisa