Gordon
Gordon

Reputation: 1651

how to trigger click button?

I have this click button code

    \$("#extractmonkeys").click(function () {
        \$("#grapharea").html(" ");
        \$("#paramselection").html(" ");

How can I trigger this code from somewhere else?

     \$("#extractmonkeys").trigger("click");

Am i missing something?

please note: \$ is because i'm coding jquery inside perl ...

Upvotes: 1

Views: 2756

Answers (4)

Aliostad
Aliostad

Reputation: 81700

To find out what's wrong

  • Check the source of the web page to make sure your code is there
  • Check the resources with fiddler, ... to make sure your jQuery script is loaded and has not got 404
  • Use javascript console in Chrome, etc to call that function and make sure it can be called
  • put an alert in your function. Perhaps it is being called but does not produce the result you want.

Upvotes: 0

David Tang
David Tang

Reputation: 93714

You're missing }); from the end of:

\$("#extractmonkeys").click(function () {
    \$("#grapharea").html(" ");
    \$("#paramselection").html(" ");

Otherwise, your \$("#extractmonkeys").trigger("click"); will work.

It could also be possible that the above code is not inside a document.ready:

\$(document).ready(function () {
    /* code here */
});

In which case "#extractmonkeys" cannot be found and thus nothing happens.

Upvotes: 1

Ivo
Ivo

Reputation: 3436

function myFunction(){

$("#grapharea").html(" "); $("#paramselection").html(" ");

}

$("#extractmonkeys").click(function () {

myFunction();

}

this way you can call your function from everywere

Upvotes: 1

SLaks
SLaks

Reputation: 888283

That code should work fine.
You can also call $(...).click().

Upvotes: 1

Related Questions