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…)
October 26, 2008 at 4:35 pm
I think that instead of doing this:
el.disabled = el.disabled ? false : true;
you could just do this:
el.disabled = !el.disabled;
December 25, 2008 at 6:13 pm
well, hi admin adn people nice forum indeed. how’s life? hope it’s introduce branch
February 19, 2009 at 6:58 pm
still not working in mozilla ??
June 2, 2009 at 12:28 pm
It was really helpful for me. Thank you vey much
June 2, 2009 at 7:12 pm
still not working in firefox
September 1, 2009 at 12:16 pm
This code is tested under FireFox 3.5.2 and IE 8.0 and in both browser it is working properly.
September 11, 2009 at 11:02 am
Its working in all browsers.Its working fine with form, input, button elements. But text inside div elements are not disabled.
September 11, 2009 at 11:24 am
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?
September 17, 2009 at 12:45 pm
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 ?
September 17, 2009 at 12:47 pm
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 ?