1

I'm looking for using Javascript in order to enable/disable submit button. This button should respect both following conditions :

  • Enable : if checkbox is checked AND dropdown list value set
  • Disable : if both or one of previous conditions are not good (checkbox unchecked or date value not set)

This is my code :

function checkValid() {
  var cbChecked = $(".fake-radio").is(":checked");  // check if checked
  var selectelem = document.getElementById('year');
  var btnelem = document.getElementById('document-button');

  btnelem.disabled = !selectelem.value;

    }

And this is my html part :

<table id="document-table">
      <thead>
          <tr>
            <th>{% trans 'Choice' %}</th>
            <th>{% trans 'Document title' %}</th>
          </tr>
      </thead>
      <tbody>
          {% for document in query_document %}
            <tr>
              <td><input type="checkbox" class="fake-radio" id="document-checkbox" name="DocumentChoice"
                         value="{{ document.id }}"></td>
            </tr>
          {% endfor %}
      </tbody>
</table>

<select id="year" name="q1year" value="{{ request.get.q1year }}" required>
    <option selected="selected">
        <option value="" selected disabled hidden></option>
    </option>
</select>

<button class="btn btn-default" id="document-button" type="submit"
                name="UpdateDocument">{% trans "Submit" %}</button>

I'm pretty new with Javascript

EDIT :

I made this, is it true ?

function checkValid() {
      var cbChecked = $(".fake-radio").is(":checked");  // check if checked

      var selectelem = document.getElementById('year');
      var btnelem = document.getElementById('document-button');
      btnelem.disabled = !selectelem.value;
      var dropdown_value = btnelem.disabled

      $("#document-button").prop("disabled", !cbChecked || dropdown_value);
    }
0

2 Answers 2

0

If I understand your situation correctly, then it looks like you have a minor logic error in your latest update. Consider revising your checkValid function like so:

function checkValid() {

  var selectelem = $('#year');
  var isChecked = $(".fake-radio").is(":checked");  // radio is checked

  var selectHasValue = !!selectelem.val(); // select has value

  // use de morgans law to compute disabled property
  $("#document-button").prop("disabled", !(isChecked || selectHasValue));
}

You need to ensure that checkValid() is called when ever relevant form inputs are changed. You can do this by adding the following script:

$(function() {

    // Apply validation logic when relevant fields change
    $('#year, .fake-radio').change(checkValid);

    checkValid(); // Apply validation logic on page load automatically
});

Also, for more on de morgans law, see this wiki article. Hope this helps!

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

2 Comments

I tried your code. I can select a date value without check my checkbox and submit button appears enable. It shouldn't be. I have to see why it appears this effect with your code.
Hi, just expanded the answer - does this help?
0

you have to add on change event for your checkbox and select. and you don't need jquery for doing this, use pure js whenever you can. this is what you need, take this as a reference and edit your code :

function checkValid(){
    var check = document.getElementById("document-checkbox").checked;
    var e = document.getElementById("year");
    var select = e.options[e.selectedIndex].value;
  
    if (select && check) document.getElementById("document-button").disabled = false 
    else document.getElementById("document-button").disabled = true
    
    
  }
<body>
    <input onchange="checkValid()" type="checkbox" class="fake-radio" id="document-checkbox" name="DocumentChoice" value="{{ document.id }}">
    <select onchange="checkValid()" id="year" name="q1year" value="{{ request.get.q1year }}" required>
        <option selected="selected">
            <option value="x">x</option>
        </option>
    </select>
    <button disabled class="btn btn-default" id="document-button" type="submit" name="UpdateDocument">submit</button>
</body>

Comments

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.