Note that there are some explanatory texts on larger screens.

plurals
  1. POChange button class when form inputs are valid
    text
    copied!<p>I have a button that submits a form. </p> <pre><code>&lt;input type="button" class="disabled" id="submitbutton" name="Continue" title="Continue" value="Continue" onclick="NextPage('CON');" /&gt; </code></pre> <p>I would like to change the class of the button automatically from "disabled" to "enabled" based on the value of inputs in my form (of which there are quite a few combinations i.e. I can't just list them all as needing a value - there are conditions to whether an input is mandatory). </p> <p>I'd like to do this in JavaScript/Ajax/JQuery so the button class change occurs as soon as all required inputs are valid, rather than some sort of refresh happening to do server side validation. </p> <p>I'm not really sure where to start:</p> <p>I had something along the lines of </p> <pre><code>function ChangeButton() { if (document.forms[0].QUESTIONID.value != "") &amp;&amp; (document.forms[0].QUESTIONIDTWO.value != "") etc etc.... { document.getElementById('submitbutton').class = 'enabled'; } } </code></pre> <p>But I couldn't get it to work - it was only working for the first value - I put an onchange="ChangeButton()" on the dropdown to validate. </p> <p>If someone could give me some tips I'd be extremely grateful! </p> <p>Thank you for the help so far - the parenthesis seems to be where I'm failing:</p> <pre><code> function ChangeButton() { if ((document.forms[0].IPR_TITL.value != "") &amp;&amp; (document.forms[0].IPR_FNM1.value != "") &amp;&amp; (document.forms[0].IPR_FNM1.value != "") &amp;&amp; (document.forms[0].IPR_SURN.value != "") &amp;&amp; (document.forms[0].IPR_GEND.value != "") &amp;&amp; (document.forms[0].IPR_DOB.value != "") &amp;&amp; (document.forms[0].IPQ_CRIM.value != "") &amp;&amp; (document.forms[0].IPQ_ETHC.value != "") &amp;&amp; (document.forms[0].IPQ_DSBC.value != "") &amp;&amp; (document.forms[0].IPQ_MARK1.value != "") &amp;&amp; (document.forms[0].IPQ_NATC.value != "") &amp;&amp; (document.forms[0].IPQ_COBC.value != "") &amp;&amp; (document.forms[0].IPQ_COD.value != "") &amp;&amp; (document.forms[0].IPQ_FIVE.value != "") &amp;&amp; (document.forms[0].IPQ_PERM.value != "") &amp;&amp; (document.forms[0].IPQ_VISF.value != "") &amp;&amp; (document.forms[0].IPQ_A_USD.value != "") &amp;&amp; (document.forms[0].IPR_HAD1.value != "") &amp;&amp; (document.forms[0].IPR_HAD3.value != "") &amp;&amp; (document.forms[0].IPR_HTEL.value != "") &amp;&amp; (document.forms[0].IPR_HAEM.value != "") &amp;&amp; (document.forms[0].IPQ_FEES.value != "") &amp;&amp; (document.forms[0].IPQ_REF1TIT.value != "") &amp;&amp; (document.forms[0].IPQ_REF1ORG.value != "") &amp;&amp; (document.forms[0].IPQ_REF1POS.value != "") &amp;&amp; (document.forms[0].IPQ_REF1AL1.value != "") &amp;&amp; (document.forms[0].IPQ_REF1AL3.value != "") &amp;&amp; (document.forms[0].IPQ_REF1AL5.value != "") &amp;&amp; (document.forms[0].IPQ_REF1EMA.value != "") &amp;&amp; (document.forms[0].IPQ_DISC.value != "")) &amp;&amp; ((document.forms[0].IPQ_PERM.value != "") &amp;&amp; (document.forms[0].IPQ_FIVE.value != "N")) &amp;&amp; ((document.forms[0].IPQ_AGNT.value != "") &amp;&amp; (document.forms[0].IPQ_A_USD.value != "Y")) &amp;&amp; ((document.forms[0].IPQ_CSTRT.value != "") &amp;&amp; (document.forms[0].IPQ_A_USD.value != "N") &amp;&amp; (document.forms[0].IPQ_CENDD.value != "") &amp;&amp; (document.forms[0].IPQ_CAD1.value != "") &amp;&amp; (document.forms[0].IPQ_CAD3.value != "") &amp;&amp; (document.forms[0].IPQ_CAD4.value != "") &amp;&amp; (document.forms[0].IPQ_CAPC.value != "") &amp;&amp; (document.forms[0].IPQ_CTEL.value != "")) &amp;&amp; ((document.forms[0].IPQ_AWDB.value != "") &amp;&amp; (document.forms[0].IPQ_FEES.value != "") &amp;&amp; (document.forms[0].IPQ_FEES.value != "Private Funds Self or Family") &amp;&amp; (document.forms[0].IPQ_AWDS.value != "")) &amp;&amp; ((document.forms[0].IPQ_RESEARCH.value = "Y") &amp;&amp; (document.forms[0].IPQ_RESSRT.value != "") &amp;&amp; (document.forms[0].IPQ_RESMOA.value != "") &amp;&amp; (document.forms[0].IPQ_RESAR.value != "") &amp;&amp; (document.forms[0].IPQ_RESDIS.value != "")) { document.getElementById('submitbutton').className = 'processbuttons'; } else { document.getElementById('submitbutton').className = 'enabled'; } } </code></pre> <p>I've tried various combinations to try and get this working but can't seem to enclose the conditions correctly with the right combination of parenthesis.</p> <p>Again if someone can point me in the right direction I'd be very grateful! </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload