1

In my models.py I have

urgency = models.BooleanField(blank=True, default=False, verbose_name="Hitno otklanjanje")

And I wish to run a javascript function to change a text field depending on 'True' or 'False' on that Boolean field. Problem is, I can't get the value. When I use document.getElementById('id_urgency').value; I always get 'on', no matter of clickbox check. Why is that? I guess I'm reading the value wrong? Or?

    <script type="text/javascript">
         function makeReadOnly()
             {
              var a = document.getElementById('id_urgency').value;
              console.log(a);

              if (document.getElementById('id_urgency').value == 'True'){
                  document.getElementById('id_date_days').readOnly = true;
             }else if (document.getElementById('id_urgency').value == 'False'){
                  document.getElementById('id_date_days').readOnly = false;
              }
             }
         document.getElementById('id_urgency').addEventListener('change', makeReadOnly);

This is a part of django form. If I go to 'inspect' on that page, that Checkbox doesn't even have value, why is that?

input type="checkbox" name="urgency" class="checkboxinput form-check-input" id="id_urgency"

1 Answer 1

2

By using .value, you get the value attached to that checkbox, not whether the checkbox is checked or not.

You can check if a checkbox is checked with:

var a = document.getElementById('id_urgency').checked;

or as specified by javascripttutorial.net:

To check if the accept checkbox is checked, you use the following code:

const cb = document.getElementById('accept');
console.log(cb.checked);

(…)

If you get the value attribute of a checkbox, you always get the 'on' string whether the checkbox is checked or not. For example:

const cb = document.getElementById('accept');
console.log(cb.value); // on
Sign up to request clarification or add additional context in comments.

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.