-2

I have two Ajax-methods in my code and I would like one of them to fire the other. It could be demonstrated like this:

 $(function() {
      //Code that "clicks #target" and triggers the mthod below.
    });

$( "#target" ).click(function() {
  alert( "The above method clicked #target" );
});

Been looking around a bit but im probably using the wrong searchterms. Thanks!

0

2 Answers 2

3

You would just do:

$("#target").click();

This will invoke the defined click function for #target

Sign up to request clarification or add additional context in comments.

2 Comments

That was fast! Thanks, i´ll mark as answer asap!
jQuery('#target').trigger('click'); does the same, using this you can trigger any event that is available for your element.
1

Try this:

$(function(){
 $( "#target" ).click(function() {
  alert( "The above method clicked #target" );
 });
});

$(function(){}); is an alias for $(document).ready(function(){});

Your click handler is correct, too.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.