2

How can I disable the show/hide button content when it's disabled? Because even if it is hidden, it is required to fill those fields.

Here is My code:

<script type="text/javascript">
    function ShowHideDiv() {
        var chkYes = document.getElementById("chkYes");
        var dvPassport = document.getElementById("dvPassport");
        dvPassport.style.display = chkYes.checked ? "block" : "none";
    }
</script>

<span></span>
<label for="chkNo">
    <input type="radio" id="chkNo" name="bill" value="Billable" onclick="ShowHideDiv()" />
   Billable
</label>
<label for="chkYes">
    <input type="radio" id="chkYes" name="bill" value="Non-Billable" onclick="ShowHideDiv()" />
    Non-Billable
</label>
<hr />
<div id="dvPassport" style="display: none">
    Firm Expense Code:
 <select name="FEC" id="txtPassportNumber" > 
  <option selected> 
    --------
  </option> 
  <option> 
    Firm expence-non-billable client Travel 8970100
  </option> 
  <option> 
    Firm expense Travel-Marketing 8970205 Personal Travel
  </option> 
 
 </select>
j08691
  • 204,283
  • 31
  • 260
  • 272
Dannyweb
  • 41
  • 5

2 Answers2

1

You can add required attribute when it will be shown or when you need to.

document.getElementById("edName").required = true;

So start without required and when person show/hide something add it.

Answer from this question.

How to set HTML5 required attribute in Javascript?

Hope this helps.

Community
  • 1
  • 1
Mykola Borysyuk
  • 3,373
  • 1
  • 18
  • 24
0

You could check the radio buttons display style and if it is none set the required attribute to false else set it to true.

document.getElementById("edName").style.display === 'none' ? document.getElementById("edName").required = none : document.getElementById("edName").required = true;

update

This should work for all the radio buttons

var radioBtns = document.querySelectorAll('[input type="radio"]');

radioBtns.forEach( function (item){
    item.getElementById("edName").style.display === 'none' ? item.getElementById("edName").required = none : item.getElementById("edName").required = true;
});
andre mcgruder
  • 1,120
  • 1
  • 9
  • 12