Click here to Skip to main content
15,885,952 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi!

I want to ask what javascript to use. Because I have two options for a certain field. One is a textbox the other is a select statement. I want the behavior of them to be: If user inputs text in the textbox the select option will be disabled, but when the user selects a value from the select statement the textbox and the text in the textbox will be disabled and removed.

HTML
<div class="form-group">
<label class="control-label col-sm-2">Event Location:</label>
<div class="col-sm-4">
  <?php 
If (!isset($_SESSION['EventLoc'])){
?>
	<input id="freetxt" type="text" name="LocOfEvent" class="form-control" size="48" value="<?php echo isset($_POST["LocOfEvent"]) ? $_POST["LocOfEvent"] : '';?>"/>
	<?php 
}
else{?>
	<input id="freetxt" type="text" name="LocOfEvent" class="form-control" size="48" value="<?php echo $_SESSION['EventLoc'];?>"/>
	<?php
}

if (isset($_POST['CheckOut']) || isset($_POST['ScanItem']) || isset($_POST['BrowseItem']))
{
	if($_POST['batchEvent'] == "")
	{
		echo "<font color='red'>*Location is Empty</font>";
		$error= true;
		$msg .= "Location is Empty\\n";
	}
}
?>
</div>
<label class="col-sm-1" align="center">-or-</label>
<div class="col-sm-5">
	<select id="options" class='form-control'>
		<option value=''>Select Location</option>
		<option value='1'>This is a test</option>
	</select>
</div>
</div>
Posted

1 solution

You can enable/disable element using the disabled DOM element property: http://www.w3schools.com/jsref/prop_html_disabled.asp[^].

You can change this property in the event handler of onchange: http://www.w3schools.com/jsref/event_onchange.asp[^].

You should better do it on client side only, to avoid unwanted postback and for better performance.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900