Enable or disable DIV tag and its inner controls using Javascript

Today, I was searching for a solution to enable/disable DIV element using javascript. I have tried so many code, but all are working in IE only and not supported by FF (firefox). At last I found some solution which is helpful. I thought this might also useful to you.

Copy/Paste following javascript code snippet.
<script type="text/javascript">
function toggleAlert() {
toggleDisabled(document.getElementById("content"));
}
function toggleDisabled(el) {
try {
el.disabled = el.disabled ? false : true;
}
catch(E){
}
if (el.childNodes && el.childNodes.length > 0) {
for (var x = 0; x < el.childNodes.length; x++) {
toggleDisabled(el.childNodes[x]);
}
}
}
</script>

Copy/Paste following HTML code in BODY tag:

<div id="content">
<table>
<tr>
<td><input type="text" name="foo" /></td>
</tr>
<tr>
<td>
<select name="bar">
<option>a</option>
<option>b</option>
<option>c</option>
</select>
</td>
</tr>
</table>
</div>
<input type="checkbox" value="toggleAlert()" onclick="toggleAlert()" />

Here, I have used one checkbox, by clicking which you can enable/disable child controls of a DIV. However, you can use any element other than checkbox (e.g button, radio button, etc…)

10 Responses to “Enable or disable DIV tag and its inner controls using Javascript”

  1. I think that instead of doing this:
    el.disabled = el.disabled ? false : true;

    you could just do this:
    el.disabled = !el.disabled;

  2. cwxwwwxdfvwwxwx Says:

    well, hi admin adn people nice forum indeed. how’s life? hope it’s introduce branch ;)

  3. still not working in mozilla ??

  4. It was really helpful for me. Thank you vey much

  5. still not working in firefox

  6. This code is tested under FireFox 3.5.2 and IE 8.0 and in both browser it is working properly.

  7. Its working in all browsers.Its working fine with form, input, button elements. But text inside div elements are not disabled.

    • Sankar,

      why do you want to disable the text inside div? I think the text is already not editable, or are you talking about the text inside textbox?

      • I have a customized rounded corner button. I am not using any form elements in div.

        Check out the below code

        SAVE

        How can i grey out this button class ?

      • I have a customized rounded corner button. I am not using any form elements in div.

        Check out the below code

        div class=”roundedbutton” onMouseOver=”this.className=’roundedbutton_hover’” onMouseOut=”this.className=’roundedbutton’”

        b class=”rb1″ /b
        div class=”rboxcontent”
        SAVE
        div
        b class=”rb1b” /b
        /div

        How can i grey out this button class ?

Leave a Reply