0

trying to use this code to simulate a click on an object, I am injecting this code in the html..

<script type='text/javascript'> 
   $(window).load(function() {
      $('#hellothere').click();
   });
</script>

Not wroking... at all!

4
  • Why not $(document).ready(function() { ... })? Commented Oct 26, 2012 at 3:41
  • What type of element is "#hellothere" and what did you expect it to do? Run attached handlers? Navigate to the url (if a hyperlink)? Commented Oct 26, 2012 at 3:42
  • jsfiddle.net/rrWhz --- it's working... at all! Commented Oct 26, 2012 at 3:42
  • no it is a textarea, i want to simulate a click which will trigger other events. but this is not the issue. Commented Oct 26, 2012 at 3:42

2 Answers 2

2

You should use the ready() function to run code when the DOM is ready - http://api.jquery.com/ready/ - the load() method is for loading new content - http://api.jquery.com/load/ - and is incorrect for your purposes. You can then use trigger() to fire a click event on a DOM object.

// run when the DOM is loaded
$(document).ready(function(){
    $('#hellothere')
         // Set up the click event
         .on('click', function(){ alert('you clicked #hellothere'); })
         // Trigger the click event
         .trigger('click');
});
Sign up to request clarification or add additional context in comments.

3 Comments

If you read the .load() doco that you linked to you'll see that there is also a .load() event handler - jQuery decides which processing to do based on what arguments are passed.
i have also tried this before posting the question, nothing seems to work. does it matter that the object i'm trying to simulate the click on is a textarea?
man if you have define a trigger first you have to define click function on that div. Like This: $(document).ready( function() { $('#hellothere').click(function() { // some functions here }); // and now if you want to trigger the click $('#hellothere').trigger('click'); }); Now as the DOM is loaded the functions you define in click function will automatically run..
0

If you are trying to simulate a click on object, you should use the trigger method,

$(function){
   $('#hellothere').trigger('click');
});

Here is the link to docs on trigger: http://api.jquery.com/trigger/

This is the code for the click method:

jQuery.fn.click = function (data, fn) {
  if (fn == null) {
    fn = data;
    data = null;
}

return arguments.length > 0 ? this.on(name, null, data, fn) : this.trigger(name);
}

as you can see; if no arguments is parsed to the function it will trigger the click event.

So use .trigger("click") cause you will call one less function. https://stackoverflow.com/a/9666547/887539

PS:

This is a nice tool to look into jQuery's source code: http://james.padolsey.com/jquery/

4 Comments

But .click() with no arguments is just a shortcut to .trigger('click').
because trigger is faster than click, i have updated my answer
OK, if performance really matters that much use .trigger(), but that doesn't explain why the OP's code doesn't work...
please post your code as fiddle, normally it works, refer this fiddle, jsfiddle.net/rrWhz

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.