I have a checkbox with the following code:
<input type="checkbox" id="check" onclick="function(this);" checked="checked" />
But in same cases I want the call to be made on page load.
How do I replicate the “this” manually?
I tried adding onload=”function(this);” to the input, but that didn’t work.
And if I just call “function();” on page load then the function naturally doesn’t work.
So it’d be great if I could manually call a function like: function(htmlElement[check]); or whatever “this” constitutes.
Hope I’ve made myself clear 🙂
In your case,
thiswill refer to theinputelement. Since that element has an ID, you can easily get a reference to it with thegetElementByIdmethod):You can place that in the
onloadattribute of thebodyelement, but ideally, stop using inline event handlers and use theaddEventListenermethod instead.