0

I am using an HTML span tag to display some text if a certain radio option is chosen. If I click on the 2nd radio option, the span tag text should be overridden and turned into an empty string. The text currently does not disappear.

I have tried the style.display = "none" and I have also tried overriding the innerHTML with an empty string.

Javascript



        if(timeCard[1].value == "N/A"){

            document.getElementById("radioError").innerHTML = "Please verify this field before submitting.";
            console.log('Hi');
        }else{
            document.getElementById("radioError").innerHTML = "";
        }

    }

HTML

<input type="radio" name="timecard" value="Yes">Yes<br>
<input type="radio" name="timecard" value="N/A">N/A<br>
<span id="radioError" style="font-size: 0.9em; color:red;"></span>

I want the span text to be overridden by the 2nd innerHTML in the javascript.

3
  • 1
    There is a lot you are not showing. How are you getting and testing the current radio option that has been selected? What does objPayCode and timeCard have to do with this? Commented Jun 25, 2019 at 21:40
  • your problem should how you check the radio box value. Please edit you question with the code about radio buttonl Commented Jun 25, 2019 at 21:45
  • I have posted the radio buttons. Commented Jun 25, 2019 at 21:50

2 Answers 2

1

Assuming timeCard is an array, instead of timeCard[1].value, you're just looking to check that timeCard[1] is equal to N/A.

timeCard = ["one", "N/A"];

if (timeCard[1] == "N/A") {
  document.getElementById("radioError").innerHTML = "Please verify this field before submitting.";
} else {
  document.getElementById("radioError").innerHTML = "Please mitting.";
}
<span id="radioError" style="font-size: 0.9em; color:red;"></span>

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

1 Comment

Hi, I tried this code but the Please Verify... font stays on the screen even when I click the other radio button.
0

Without complete code, at a guess, you should be using .checked property to see if it is selected instead of value given that value for timeCard[1] will always be "N/A".

if (timeCard[1].checked) {
   document.getElementById("radioError").innerHTML = "Please verify this field before submitting.";
    }else{
        document.getElementById("radioError").innerHTML = "";
    }

1 Comment

This is the correct way to check which radio button is checked.

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.