Jump to content


Photo

Help with HTML form


  • Please log in to reply
3 replies to this topic

#1 DarkHorizon

DarkHorizon
  • Members
  • PipPip
  • Member
  • 11 posts
  • LocationScotland

Posted 03 August 2006 - 09:48 AM

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 not checked then the user can fill in the text field instead.

is this possible with a bit of javascript?

cheers
DH

#2 manmadareddy

manmadareddy
  • Members
  • PipPipPip
  • Advanced Member
  • 50 posts

Posted 03 August 2006 - 10:26 AM

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>

#3 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 03 August 2006 - 10:30 AM

Umm, took too long. heheh, ohwell

This is possible with javascript:
<!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>


#4 DarkHorizon

DarkHorizon
  • Members
  • PipPip
  • Member
  • 11 posts
  • LocationScotland

Posted 03 August 2006 - 10:52 AM

many thanks guys

i worked through manmadareddy's example first and modified it for 2 seperate check boxes and 18 inputs..

works perfectly.

i have a few more questions...post them soon...cheers DH




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users