I am trying to get the text within the input tags to display the status of the radio button. The code works fine except for the very first time (the onload event). When the page loads the first alert is displayed but not the second. Once I click on the other radio button (the unchecked one) and then back to the original one, I see both alerts. Can someone please help me with why this is happening? I am sure it is something stupid. Thanks
<script>
window.onload = autoSelect("rolls");
function autoSelect(theValue)
{
if(theValue=="rolls")
{
alert("hello world");
document.getElementById("theLabel").innerHTML=theValue;
alert("hello World");
}
else
{
document.getElementById("theLabel").innerHTML=theValue;
}
}
</script>
<div >
<form action="" >
<input type="radio" id="matRadioRolls" name="rollsOrSqFeet" value="rolls" onclick="autoSelect(this.value)" checked>Rolls<br/>
<input type="radio" id="matRadioSqFt" name="rollsOrSqFeet" value="sqfeet" onclick="autoSelect(this.value)" >Sq Ft<br/>
</form>
<label class="productsLabel" for=rolls" id="theLabel"></label>
<input type="text" name="rolls" id="rollsMat"></button>
</div>
<label class="productsLabel" for="rolls" id="theLabel"></label>. Seems to work here: jsfiddle.net/xXLAd