jQuery simulate click jQuery simulate click jquery jquery

jQuery simulate click


You either have to make your function anonymous:

$('#button').click(function() {    //...some code});

Or pass the function itself:

function getType() {    //...some code}$('#button').click(getType);

If you just want to trigger a click, call .click():

$('#button').click();

Also, your id parameter won't be the element's id. It'll be the click event object. To get the element's id, you can refer to the clicked element using this:

$('#button').click(function() {    var id = this.id;});

I suggest you read a few JavaScript and jQuery tutorials (in that order).


You are using the inline notation so, you should use an anonymous function (no name function)

your code should be:

$('#button').click(function() {      // do your stuff here    });

Beside that, as the titles says, you need to simulate a click event, right ? if so you better use something like:

$('#button').on('click', function() {  alert($(this).text());});// somewhere when you want to simulate the click you call the trigger function$('#button').trigger('click');

see documentation here


$('#button').click(function getType(id) {    //...some code});

Should be:

$('#button').click(function() {        [...] code here    });

function() { } is a callback with what code have to do when I click some element.

If you have the getType function, you can pass it as a callback:

$('#button').click(getType);

If you want to trigger a funcion, when page load, you can do this:

$('#button').trigger('click');

Or

function getType() {    [...] code here}getType();