-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,691
  • 8
  • 33
  • 60

2 Answers2

3

You would just do:

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

This will invoke the defined click function for #target

Mauno Vähä
  • 9,688
  • 3
  • 33
  • 54
tymeJV
  • 103,943
  • 14
  • 161
  • 157
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,193
  • 1
  • 15
  • 22