Reputation: 31
I am trying to do make the radio button selected by default on page load.
From the below screen it is a frame which is loaded. I am trying to make the radio buttons enable by using
document.formName.radioButtonId[1].checked = true;
Where formName is my form name that I declared in html, radioButtonId is id for radio button I used in html.
<div>
<form name = "formName">
.
.
.
.
<div>
<input type = "radio"
id = "radioButton"
.
.
.
</div>
</form>
</div>
So my expectation is from the above code Joe radio button has to be enabled.
But the html is not completely loaded and when I do
document.formName.radioButtonId[1].checked = true;
the error on console says radioButton
was undefined because of the form is not available.
I am not able to access any of the elements from form
until it is completely loaded. Is there any way that I can check for the page title is loaded or visible then do the condition that I want to do?
I tried
if (document.readyState === "complete") { //do something }
This didn't work because document.readyState is always complete
as the document is referring to parent document from the screenshot you can see it was referring to background page.
Once the Modify Name is completely loaded then I was able to access formName form and I was able to make Joe
as selected using document.formName.radioButtonId[1].checked = true;
I am making Joe as checked based on condition from the code that I have (fyi). That might change so want to be generic.
[![Modify Name is child to background image like a popup][2]][2] Need help on how to make Joe as checked after loading the page automatically.
Upvotes: 0
Views: 3746
Reputation: 61
<input type = "radio" id = "radioButton" checked>
OR in Javascript
window.onload = function() {..}
Upvotes: 0
Reputation: 131
You would write in your html <input type="radio" id="radioButton" checked />
. That's much less complicated than using javascript to do it.
Upvotes: 1
Reputation: 187
Just put checked inline HTML :
e.g:
<input type = "radio" id = "radioButton" value="Joe" checked>Joe
Upvotes: 1