Jump to content

how 2 hide one text field in a form when you select a specific item in combo box


Kunkka

Recommended Posts

i want to hide one text field in form when i select a specific item in the combo box.

 

im creating a online mcq paper. so when in the registration process i have put a combo box to select either lecture or student. so if user logs in and selects lecture. they should get extra text field. which others not getting. can someone help me

This can be done easier with JavaScript. I would suggest checking out the jQuery JavaScript Library. It simplifies the everyday, mundane tasks usually done in JS. It also makes it a lot easier to learn JS if you dont already know it.

Here is the Jquery website.

can someone provide me with the code. how to do it

 

The code:

 

<html>
<head>
<title>Sample</title>
<style type="text/css">
<!--
  #idName {
     display: block;
  }
-->
</style>
<script type="text/javascript">
<!--
function doToggleTextBox(objSelect, theId) {
    var obj = document.getElementById(theId);
    if ( objSelect.value == '2' ) {
       obj.style.display = 'none';
    }
    else {
       obj.style.display = 'block';
    }
}
//-->
</script>
</head>
<body>
   <form id="idForm" name="fromReg" method="post" action="register.php">
      <select name="items" onChange="javascript:doToggleTextBox(this, 'idName')">
          <option value="1">Item 1</option>
          <option value="2">Item 2</option>
      </select>
      <br />
      <div id="idName"><label>Enter name: <input type="text" id="Name" /></label><br /></div>
      <div id="idSubject"><label>Enter subject: <input type="text" id="Subject" /></label><br /></div>
   </form>
</body>
</html>

 

The idea in the above code is, on selection of "Item 2" from the select box, the line, "Enter name" will be hidden.

 

Does it help?

 

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.