Reputation: 5
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
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
Reputation: 9414
Don't click the button, run the function.
$(document).ready(function() {
$('#id').click();
// Or
click_function();
});
Upvotes: 0
Reputation: 3965
You can use jquery to do this very easy: // every thing is loaded
$(function(){
$('#id').click();
});
Upvotes: 0
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
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