tags:

views:

70

answers:

3

hi,

I cannot trigger this click on this element

$(this).find('.views-field-field-cover-fid').find('a.imagecache-coverimage').click();

The jQuery path is correct. Indeed if I print it, it gives the correct a element:

console.log($(this).find('.views-field-field-cover-fid').find('a.imagecache-coverimage'));

But for some reason the function click() doesn't work on it. thanks

+3  A: 

What about a single find?

$(this).find('.views-field-field-cover-fid a.imagecache-coverimage').click(function() {
  // stuff
});
Alec
Indeed. Avoid overuse of `find`. It is very process-intensive.
dclowd9901
thanks for the tip. I still haven't solve how to simulate a click() event on the <a /> element, in order to visit the link.
Patrick
+4  A: 

http://api.jquery.com/trigger/

Edit: OK, now I understand what you want to do, and sorry that I didn't realize .click() and .trigger('click') are basically equivalent. Another method is to bind the address to the click event.

$(this).find('.views-field-field-cover-fid a.imagecache-coverimage')
       .bind("click", function(){
           window.location.href = $(this).attr('href');
       });

Then you'll be able to trigger the click!

$(this).find('.views-field-field-cover-fid a.imagecache-coverimage')
       .trigger('click');

(or .click();)

Demonstration

ghoppe
I think what ghoppe is trying to say is that you're probably not using the right syntax for what you want to do. You need to bind a `click` event to your object (something that happens when it's clicked), and then to "trigger" that action elsewhere (without actually performing the specific act), you need to use the syntax `$('.the_element').trigger('click')`. This will activate the click that's currently bound to classes of `the_element`.
dclowd9901
@ghoppe - This "answer" is wrong if you're suggesting you need to use `.trigger()` instead of `.click()` to fire the event. @dclowd9901 - You do not need to use `$('.the_element').trigger('click')` to trigger the event. You can use `$('.the_element').click()` as well.
patrick dw
I want to simulate a click() event on the <a /> element, in order to visit the link.
Patrick
@patrick Thanks for setting me straight. Woah. Confusing that @Patrick asked the question and @patrick answered. ;-)
ghoppe
+4  A: 

EDIT:

Now I see that you want to visit the href of the a element.

Do this:

window.location = $(this).find('.views-field-field-cover-fid').find('a.imagecache-coverimage').attr('href')

Patrick, the .click() function behaves differently depending upon how it is used.

If you are hoping to fire a 'click' event handler for the element you selected, then you are using it correctly, but first you will need to give some functionality to that element.

That brings us to the second (and more common) way in which .click() is used. That is to give functionality to an element. Run this when the DOM loads:

$(document.ready(function() {
    $('.views-field-field-cover-fid')
           .find('a.imagecache-coverimage')
           .click(function() {
                alert('I was clicked!');
            });
 });

Now all elements that match your selector will show the alert when clicked, or when you trigger the event like you did originally.

$(this).find('.views-field-field-cover-fid')
       .find('a.imagecache-coverimage')
       .click();
patrick dw
I also misunderstood the question.
ghoppe