0

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>

1
  • Might be the type: <label class="productsLabel" for="rolls" id="theLabel"></label>. Seems to work here: jsfiddle.net/xXLAd Commented Nov 26, 2013 at 18:23

2 Answers 2

2

You're actually executing your function call before the window object has fully loaded. You should change the call to this:

window.onload = function(){autoSelect("rolls");}

jsFiddle example

Oh, and a small side note. You have a typo in for=rolls" (missing a set of quotes).

Sign up to request clarification or add additional context in comments.

1 Comment

Yep I sure do. Didn't notice that. Thanks very much and works perfectly now.
0

Just guessing here: Depends on what browser you are using. there is a chance that the browser supresses the second alert (because you clicked don't show again)

Generally: Use console.log over alert. Use the Dev Tools.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.