-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!

user2915962
  • 2,641
  • 7
  • 30
  • 57

2 Answers2

3

You would just do:

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

This will invoke the defined click function for #target

Mauno Vähä
  • 9,560
  • 3
  • 28
  • 54
tymeJV
  • 102,126
  • 13
  • 159
  • 155
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.

Kyle Emmanuel
  • 2,111
  • 1
  • 14
  • 21