0
$('#add-to').click(function(e){
      addNewLine(e,this.id);
});

the above code works when i click the particular element with id add-to

now I need to automatically trigger this click even and call the same method (addNewLine(e,,this.id)) on click with parameters on loading the page .

Thanks for helping.

Refer Here

1
  • 1
    Use $('#add-to').click() or $('#add-to').trigger('click') on page load it will fire click event Commented Jun 11, 2014 at 9:56

2 Answers 2

1

First off all what do you mean by automatic trigger?. You trigger some event when something happens like document is loaded etc

Let this event be like this only:

    $('#add-to').on('click',function(){
          addNewLine(e,,this.id);
    });

and whenever you have to trigger that element use:

     $('#add-to').trigger('click');

On triggering addNewLine() function will automatically called because its inside click event and whenever you trigger click, Click event you assigned earlier is also fired.

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

1 Comment

This will call addNewLine() twice, wouldn't it?
0

Try

$(document).ready(function(){
    $('#add-to').click() 
 });

or

$(document).ready(function(){
    $('#add-to').trigger('click')
 });

4 Comments

but where i am calling the addNewLine method here ?
whenever you click that #add-to that will automatically call the assigned event that is $('#add-to').click(function(e){addNewLine(e,,this.id); });
not manually, on page load automatically trigger the click event
it will call install the code in your file and check

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.