...

View Full Version : Disabling text boxes



greenspacechunk
07-12-2007, 06:36 PM
I have the following text box and button:


<input type="text" name="ContainerCode" maxlength="50"/>
<input type="button" name="Container_additem" value="..." onclick="Popup('ContainerCode');"/>

And the following checkbox and function:

<input name="independentBox" type="checkbox" onclick=check_independent_status() value="0"/>

function check_independent_status() {
if(document.BarCodeForm.independentBox.checked==1)
{document.BarCodeForm.Container_additem.disabled= true;
document.BarCodeForm.ContainerCode.disabled= true;}
else{
document.BarCodeForm.ContainerCode.disabled= false;
document.BarCodeForm.Container_additem.disabled= false;}
}

When I click the checkbox, I want both the text box and button to be disabled. However, only the button is getting disabled in IE with no errors thrown. In Firefox, it works perfectly! What am I missing here???

glenngv
07-12-2007, 07:59 PM
Is there any error message? Look at the status bar if it has the "!" icon and then double-click it.

BTW, the code can be simplified like this.

<input name="independentBox" type="checkbox" onclick="check_independent_status(this)" value="0" />

function check_independent_status(oChk) {
oChk.form.Container_additem.disabled = oChk.checked;
oChk.form.ContainerCode.disabled = oChk.checked;
}

Kor
07-12-2007, 08:07 PM
could be some syntax errors

1. The XHTML end code needs a space before the slash:


<tag />


2. Better nest the handler into quotes:


...onclick="check_independent_status()"...

3. Special characters might bring errors when used in name. Better use JSON:


document['form_name']['element_name']

4. You may use the this self reference and use straight the Boleean values

Try this (untested):


<input name="independentBox" type="checkbox" onclick="check_independent_status(this)" value="0" />
...........
function check_independent_status(c) {
c.form['Container_additem'].disabled=c.checked;
c.form['ContainerCode'].disabled=c.checked;
}


And, important: If you have an XHTML Doctype, the embeded javascript code must be isolated inside a CDATA island (to prevent XML conflicts)


<script type="text/javascript">
/*<![CDATA[*/
... your code here ...
/*]]>*/
</script>

Arbitrator
07-12-2007, 09:34 PM
3. Special characters might bring errors when used in name. Better use JSON:


document['form_name']['element_name']
One could also use the DOM. The name attribute is deprecated for use on the form element anyway.


document.getElementById(elementId)

Alternatively:


document.getElementsByName(elementName)[index]



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum