I have a form with 2 radio buttons, that if I choose "Yes", a DIV with aditionally 2 pairs of radio buttons appears, and if yes is choosen in the first, I need required set in the 2 new radio button pairs ("Type" and "What to do"). If "Move Number" is choosen in "What to do" radio pair, a second DIV appears with an input field where the user should tell which number to move .. this field then needs required set .. how can I do this dynamically?
Just for info, I use Parsley JS Validator to validate the fields.
Here is a minimalistic piece of the code:
function CompanyMobileFunction() {
var x = document.getElementById("CompanyMobile");
if (x.style.display === "none") {
x.style.display = "block";
} else {
x.style.display = "none";
}
}
function CompanyMobileMoveFunction() {
var x = document.getElementById("MobileMove");
if (x.style.display === "none") {
x.style.display = "block";
} else {
x.style.display = "none";
}
}
function CompanyMobileFunctionClose() {
var x = document.getElementById("CompanyMobile");
if (x.style.display === "none") {
x.style.display = "none";
} else {
x.style.display = "none";
}
}
function CompanyMobileMoveFunctionClose() {
var x = document.getElementById("MobileMove");
if (x.style.display === "none") {
x.style.display = "none";
} else {
x.style.display = "none";
}
}
<table style="width: 550px;">
<tr>
<td style="width: 250px;"><b>Mobile phone? (required) :</b></td>
<td>
<label class="container">Yes
<input type="radio" id="CompanyMobileYES" name="CompanyMobile" onclick="CompanyMobileFunction()" value="yes">
<span class="radio"></span>
</label>
</td>
<td>
<label class="container">No
<input type="radio" id="CompanyMobileNO" name="CompanyMobile" onclick="CompanyMobileFunctionClose()" value="yes">
<span class="radio"></span>
</label>
</td>
</tr>
</table>
<div id="CompanyMobile" style="display: none;">
<table style="width: 550px;">
<tr>
<td style="width: 250px;"><b>Type (required) :</b></td>
<td>
<label class="container">iPhone
<input type="radio" id="iPhone" name="MobileOS" value="iPhone">
<span class="radio"></span>
</label>
</td>
<td>
<label class="container">Android
<input type="radio" id="Android" name="MobileOS" value="Android">
<span class="radio"></span>
</label>
</td>
</tr>
<tr>
<td style="width: 250px;"><b>What to do? (required) :</b></td>
<td>
<label class="container">New Number
<input type="radio" id="CompanyMobileMoveNew" name="CompanyMobileMove" onclick="CompanyMobileMoveFunctionClose()" value="newnumber">
<span class="radio"></span>
</label>
</td>
<td>
<label class="container">Move Number
<input type="radio" id="CompanyMobileMoveMove" name="CompanyMobileMove" onclick="CompanyMobileMoveFunction()" value="movenumber">
<span class="radio"></span>
</label>
</td>
</tr>
</table>
<div id="MobileMove" style="display: none;">
<table style="width: 550px;">
<tr>
<td style="width: 250px;"><b>Number to move (required) :</b></td>
<td align="left">
<input class="form-control" style="width: 130px;" type="number" id="mobiletomove" name="mobiletomove" minlength="11111111" maxlength="99999999">
</td>
</tr>
</table>
</div>