user3030521
user3030521

Reputation: 5

How to execute click function with javascript?

I have a button in HTML:

 <button type="button" id="id" onClick="function()">text</button>

The function simply alters the CSS and text of some HTML elements.

How do I, by default every time the page loads, "click" on the button with Javascript, without the user having to click on the button first?

Thanks in advance.

Upvotes: 0

Views: 135

Answers (6)

Jaiesh_bhai
Jaiesh_bhai

Reputation: 1814

window.onload=function;

function would be the name of your function. You don't need to click, you just need to run the function.

Upvotes: 0

Dan Grahn
Dan Grahn

Reputation: 9414

Don't click the button, run the function.

$(document).ready(function() {
  $('#id').click();

  // Or

  click_function();
});

Upvotes: 0

Ringo
Ringo

Reputation: 3965

You can use jquery to do this very easy: // every thing is loaded

 $(function(){
      $('#id').click();
    });

Upvotes: 0

Praveen
Praveen

Reputation: 56509

function is a reserved keyword. It is an error, so make some name onClick="function clickEvent()"

<script>
function clickEvent() {
   alert("I'm clicked");
}
</script>

Upvotes: 0

user1636522
user1636522

Reputation:

Using jQuery you can do this :

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

http://api.jquery.com/click/

Upvotes: 1

moonwave99
moonwave99

Reputation: 22817

$(function(){

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

});

But you should not use intrusive javascript, better do:

<button type="button" id="id" >text</button>

...

$(function(){

  $('#id').click(function(e){

    // your code here

  }).trigger('click');

});

Upvotes: 1

Related Questions