0

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.

1
  • Don't click the button, run the function. Run Code on Page Load Commented Nov 26, 2013 at 13:33

6 Answers 6

1
$(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');

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

2 Comments

Sorry, what do you mean by intrusive javascript?
@user3030521 onclick=stuff.
1

Using jQuery you can do this :

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

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

Comments

0

Don't click the button, run the function.

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

  // Or

  click_function();
});

Comments

0

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

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

Comments

0

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

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

Comments

0
window.onload=function;

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

Comments

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.