forms - Hideshow div with javascript - Stack Overflow

I would like to show a form field textbox only when a certain checkbox is checked, and hide it when it

I would like to show a form field textbox only when a certain checkbox is checked, and hide it when it is unhecked. I have gotten the code to make it hide when I check the box, but the textbox is displayed when the page is loaded until I check the box, which is opposite of what I would like to acplish. I am pretty newbish with javascript so I am sure this is pretty elementary. Thanks for any help.

HTML:

<input type="checkbox" onclick="ShowCCFields(this.value);" name="CAT_Custom_510969" id="CAT_Custom_510969_7" value="Other..." />
Other...

<div class="item" id="other">
    <label for="CAT_Custom_510972">Other:</label><br />
    <textarea onkeydown="if(this.value.length&gt;=4000)this.value=this.value.substring(0,3999);" class="cat_listbox" rows="4" cols="10" id="CAT_Custom_510972" name="CAT_Custom_510972"></textarea>
</div>  

javascript:

// Hide or show textarea if the other checkbox field is checked
        var voucher = document.getElementById("CAT_Custom_510969_7");
        function ShowCCFields(val) {                            
            if (!document.getElementById('other'))
                return;         
            if (val == voucher.checked)
                document.getElementById('other').style.display = 'inline';              
            else
                document.getElementById('other').style.display = 'none';
        }   

I would like to show a form field textbox only when a certain checkbox is checked, and hide it when it is unhecked. I have gotten the code to make it hide when I check the box, but the textbox is displayed when the page is loaded until I check the box, which is opposite of what I would like to acplish. I am pretty newbish with javascript so I am sure this is pretty elementary. Thanks for any help.

HTML:

<input type="checkbox" onclick="ShowCCFields(this.value);" name="CAT_Custom_510969" id="CAT_Custom_510969_7" value="Other..." />
Other...

<div class="item" id="other">
    <label for="CAT_Custom_510972">Other:</label><br />
    <textarea onkeydown="if(this.value.length&gt;=4000)this.value=this.value.substring(0,3999);" class="cat_listbox" rows="4" cols="10" id="CAT_Custom_510972" name="CAT_Custom_510972"></textarea>
</div>  

javascript:

// Hide or show textarea if the other checkbox field is checked
        var voucher = document.getElementById("CAT_Custom_510969_7");
        function ShowCCFields(val) {                            
            if (!document.getElementById('other'))
                return;         
            if (val == voucher.checked)
                document.getElementById('other').style.display = 'inline';              
            else
                document.getElementById('other').style.display = 'none';
        }   
Share Improve this question asked Nov 5, 2013 at 20:59 PhordenPhorden 1,0344 gold badges20 silver badges45 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 2

Why not set the display to none to begin with? You can do that in CSS or just add it to

<div class="item" id="other" style="display: none;">

Set the style to none in the HTML, just like you do in the javascript:

<div class="item" id="other" style="display:none;">

Set Display to none in the control you want to hide :

<div class="item" id="other" style="display: none;">

And In your js Function :

document.getElementById('other').style.display = 'inline';              

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745597925a4635227.html

相关推荐

  • forms - Hideshow div with javascript - Stack Overflow

    I would like to show a form field textbox only when a certain checkbox is checked, and hide it when it

    10小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信