Jump to content

Help with HTML form


DarkHorizon

Recommended Posts

Hi,
i dont know if this can be done but here goes:

i have a form with two inputs. One is a checkbox and one is a text field.
If the check box is checked, the text field gets greyed-out, disallowing user input to that field.


If the check box is [i]not[/i] checked then the user can fill in the text field instead.

is this possible with a bit of javascript?

cheers
DH
Link to comment
https://forums.phpfreaks.com/topic/16432-help-with-html-form/
Share on other sites

try this
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
<SCRIPT LANGUAGE="JavaScript">
<!--
function checksub(obj)
{
if(obj.checked==true)
{
document.frmtest.txt_once.disabled=true;
}
else
{
document.frmtest.txt_once.disabled=false;
}
}
//-->
</SCRIPT>
</HEAD>
<BODY>
<FORM name='frmtest' METHOD=POST ACTION="" >
<INPUT TYPE="checkbox" NAME="chk_once" onclick="return checksub(this);">checkbox <INPUT TYPE="text" NAME="txt_once">
</FORM>
</BODY>
</HTML>
Link to comment
https://forums.phpfreaks.com/topic/16432-help-with-html-form/#findComment-68433
Share on other sites

[b]Umm, took too long. heheh, ohwell[/b]

This is possible with javascript:
[code]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Javascript Test</title>

<script type="text/javascript">

// this function is called evertime the checkbox is changed
function disableBox(box)
{
    // get checkBox status
    var checkBox = document.form1.chkbox.checked;

    // get form to disable:
    var box2Disable = document.getElementById(box);

    // if checkbox is checked
    if( checkBox == true) {
        // disable the input field
        box2Disable.disabled = true;
    } else {
        // enable the box
        box2Disable.disabled = false;
    }
}

</script>

</head>
<body>

<form name="form1">
Checkbox: <input type="checkbox" name="chkbox" onclick="disableBox('box1')" /><br />
Box: <input type="text" id="box1" />
</form>

</body>
</html>[/code]
Link to comment
https://forums.phpfreaks.com/topic/16432-help-with-html-form/#findComment-68436
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.