I have the following function which is activated on click:
$('.results .view-rooms').click(function(){ }
Is there anyway I can trigger this function on document load?
$('.results .view-rooms').click()
You can put it in DOM ready:
$(function(){
$('.results .view-rooms').click()
});
Or window load:
$(window).load(function(){
$('.results .view-rooms').click();
});
Note that there is no such event document load.
We have DOM ready or window load
$(document).ready(fn) is exactly the same as $(fn), the second is an alias to the first.Considering that you're already using jQuery to bind the event handler, and assuming that code is already in a position where the entire DOM has been constructed, you can just chain the call to .click() to then trigger that event handler:
$('.results .view-rooms')
.click(function(){...}) //binds the event handler
.click(); // triggers the event handler
The best way is:
html form:
<form action="https://stackoverflow.com" method="get">
<button id="watchButton"></button>
</form>
End Jquery:
<script>
$('document').ready(function() {
$('#watchButton').click();
});
</script>
JQuery Version:
https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js
document readyorwindow loadyou mixed them... :)