dr1054
dr1054

Reputation: 89

adding conditions to required attribute

I have a form with mainly all simple textboxes and I am using the required attribute for the fields that are required. However, I have a textarea that only needs to be required if the previous radio button is selected as "Yes", otherwise I don't want the textarea to be required. Here is the code for those 2 fields:

<input id="ElecWiFi" name="ElecWiFi" required type="radio" 
value="Yes" />
<textarea id="WiFiNeeds" cols="50" name="WiFiNeeds" rows="3" 
placeholder="Electricity or Wi-Fi Needs"></textarea>

How would I make the textarea required only if Yes was selected in ElecWiFi field? Thanks in advance for any help.

dr1054

Upvotes: 2

Views: 3078

Answers (1)

Prasun Jajodia
Prasun Jajodia

Reputation: 470

add javascript to the click attribute like so

<input id="ElecWiFi" name="ElecWiFi" required type="radio" value="Yes" onclick="document.getElementById('WiFiNeeds').setAttribute('required', 'required')" />

Upvotes: 1

Related Questions