Mywebdunia's Blog

Show/Hide Div Block with checkbox

Posted by: Amit Nazare on: July 16, 2009


Here is another tutorial for JavaScript. How to show/hide your div block with check box.

Its very easy and simple to understand. Just copy the below code and try.

JavaScript :

<script type=”text/javascript”>
function showMe (ids, box) {
var vis = (box.checked) ? “block” : “none”;
document.getElementById(ids).style.display = vis;
}
</script>

Here the above JavaScript code will check whether the checkbox is checked or not. If it is checked then it will return true and the block of code will be displayed. Else the block of code will be hidden(display:none)

Down below is the HTML code which will show you how to call the JavaScript function.

HTML Code :

<input type=”checkbox” name=”vendors” onclick=”showMe(‘viewVendors’, this)” /> View Vendors
<div style=”display:none;” id=”viewVendors”>
Block of code goes here
</div>

Try this code and get me your feedbacks, if any errorsor if it is not working let me know.

2 Responses to "Show/Hide Div Block with checkbox"

Great! Didn’t work at first because I copied and pasted the above code directly into my code, but after changing the fancy “curly” quotes to straight quotes it worked like a charm. Thank you!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.