Jump to content

Hide/Show textbox depending on checkbox value


elmas156

Recommended Posts

Hello everyone, I don't know much about javascript but I managed to put the following code together:

 

<head>

<script type="text/javascript">
function checkValue() {

var linkemail=document.forms["theForm"]["linkemail"].value
if (linkemail == 'y') 
{
	document.getElementById('xtraInfo').style.display='';
}
else
{
	document.getElementById('xtraInfo').style.display='none';
}	
}	

</script>

</head>

<body>

<form id="theForm" name="theForm" method="post" action="">
  
  <input type="checkbox" name="linkemail" value="y" onClick="return checkValue(this)" /> Check here to link your email.
  
  <div id="xtraInfo" style="display:none;">
  Email <input name="name" type="text" id="name"  />
  </div>
  
</form>

</body>

 

I'm trying to get the textbox to show when the checkbox is selected (which DOES work) and the textbox to disappear when the checkbox is deselected (which is where I'm having the trouble.  Can anyone help me figure this out please?... Thanks for any help!

I think you need to look at the checked attribute of the checkbox rather than the value.

 

function checkValue() {
var linkemail=document.forms["theForm"]["linkemail"].checked
if (linkemail) {
	document.getElementById('xtraInfo').style.display='';
} else {
	document.getElementById('xtraInfo').style.display='none';
}	
}	

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.