javascript Hide/show div on checkbox: checked/unchecked

前端 未结 4 1635
我寻月下人不归
我寻月下人不归 2020-12-19 19:24

I am trying to make a function in javascript, that will hide / show particular div in my registration form, depending on the state of my checkbox (checked or not). Here is

4条回答
  •  长情又很酷
    2020-12-19 19:45

    You could use

    var elem = document.getElementById('powermail_fieldwrap_331');
    document.getElementById('powermail_field_doruovaciaadresa2_1').onchange = function() {
        elem.style.display = this.checked ? 'block' : 'none';
    };
    

    Demo


    If you want to hide it by default, you could use #powermail_fieldwrap_331{display:none;}. But if you want to be sure, better use

    var elem = document.getElementById('powermail_fieldwrap_331'),
        checkBox = document.getElementById('powermail_field_doruovaciaadresa2_1');
    checkBox.checked = false;
    checkBox.onchange = function doruc() {
        elem.style.display = this.checked ? 'block' : 'none';
    };
    checkBox.onchange();
    

    Demo

提交回复
热议问题