Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery: call click(); from another click();

Tags:

jquery

click

Is it possible to do something like this

$('#idone').click(function(){
 alert('we do stuff!');
});

$('#idtwo').click(function(){
 $('#idone').click();
});

... i guess not, but are there any possible workarounds?

!UPD:

Well, ok. It's a little bit more complicated. I have a jCarousel analog installed - JQueryTOOLS - Scrollable - I guess that's what its name the link: http://flowplayer.org/tools/demos/scrollable/gallery.html

I presume that it has click() event somehow hardcoded in it and when i click thumbs the plugin scrolls it to center position. Then I've bound another click event, so that I can display large image. It works similar to the example in the link above

Now I have to make this big image clickable, so clicking it I can proceed to next image in gallery, I did that, but now I have to make the carousel scroll automatically to the next thumb when I am clicking this big image. So, if I simplify my code, the essence is still smth like that:

$('#idone').click(function(){
 alert('we do stuff!');
});

$('#idtwo').click(function(){
 $('#idone').click();
});

where #idone is a thumb in the gallery, #idtwo is that preview image. I dont know what function is bound to click by default. Another thing: my guess was that binding the same event to the same object should replace the previous - it appears not necessarily.. And I'm pretty sure that it is exactly click();, not mouseup\down etc. cause calling

$('#idone').click();

on page load does the trick and carousel scrolls to the thumb with id="idone"

just in case: jquery-1.3.2.js

thanks everyone for your answers, and excuse me my lame English, no time for brushing up (:

!UPD:

ok! so I gave up on this one )) But to close this question and put an end to this discussion i have to know. All you people, who wrote that it is possible and suggested variants. HAVE YOU TRIED THEM?! Do they really work for you? Is it me, who is wrong? And - Thanks anyway!

like image 944
dr3w Avatar asked Dec 28 '09 07:12

dr3w


People also ask

How do you call a click method in jQuery?

To trigger the onclick function in jQuery, click() method is used. For example, on clicking a paragraph on a document, a click event will be triggered by the $(“p”). click() method. The user can attach a function to a click method whenever an event of a click occurs to run the function.

How do you trigger a click button?

and a trigger to execute the button1 click event handler. $("#button2"). bind("click", (function () { alert("Button 2 is clicked!"); $("#button1").

How do I trigger a click event without clicking?

If you want native JS to trigger click event without clicking then use the element id and click() method of JavaScript.

What is Click () method?

The click() method simulates a mouse-click on an element. This method can be used to execute a click on an element as if the user manually clicked on it.


2 Answers

This would be another possiblity:

$('#idone').click(function(){
 alert('we do stuff!');
});

$('#idtwo').click(function(){
 $('#idone').trigger('click');
});

edit: I just see that your code should work like that already.

like image 131
RamboNo5 Avatar answered Oct 18 '22 21:10

RamboNo5


Define a separate function which is called by both events?

function doSomething() {
 alert('we do stuff!');
}

$('#idone').click(doSomething);    
$('#idtwo').click(doSomething);

or simply

$('#idone, #idtwo').click(doSomething);

[EDIT - jQuery Scrollable]

Have you tried using this with the thumbnails in jQuery Scrollable:

$('#idone').next(); 

(I presume #idone is scrollable)

like image 34
Groo Avatar answered Oct 18 '22 19:10

Groo