Hi!

I am trying to manipulate the value of an input box based on whether a checkbox is checked or not.
example
checked input = 1600 and enabled
unchecked input = 0 and disabled

I am having 2 problems:
1) When the page is loaded I get unchecked and input = 1600: it should be : either
checked input = 1600 and enabled
or
unchecked input = 0 and disabled

2) When the checkbox is checked for the 2nd time the input remains 0 it should go back to the initial value 1600

can anyone help me?

<script language="JavaScript">
<!--
function enable_text(status)
{
if (status== true){
var var_gate_request_original = document.f1.GateRequest0.value ; 

document.f1.GateRequest0.disabled =false;
document.f1.GateRequest0.value = var_gate_request_original ;
}

if (status== false){

var var_gate_request_original = document.f1.GateRequest0.value ; 
var var_gate_request_zero = 0 ;
	
document.f1.GateRequest0.value = var_gate_request_zero;
document.f1.GateRequest0.disabled =true;
}


}
//-->
</script>
<form  name=f1  method='post'>
<table width="200" border="1">
  <tr>
    <td><input name="R1" type="checkbox" value=""  onclick='enable_text(this.checked)'/></td>
    <td><input name="GateRequest0" type="text" value="1600" /></td>
  </tr>
</table>
</form>

Recommended Answers

All 2 Replies

Hi,

Check this out.

<script language="JavaScript">
<!--
function enable_text(status)
{
if (status== true){
var var_gate_request_original  = 1600;
document.f1.GateRequest0.disabled =false;
document.f1.GateRequest0.value = var_gate_request_original ;
}

if (status== false){

var var_gate_request_original = document.f1.GateRequest0.value ; 
var var_gate_request_zero = 0;
	
document.f1.GateRequest0.value = var_gate_request_zero;
document.f1.GateRequest0.disabled =true;
}


}
//-->
</script>

<form  name=f1  method='post'>
<table width="200" border="1">
  <tr>
    <td><input name="R1" type="checkbox" value=""  onclick='enable_text(this.checked)'/></td>
    <td><input name="GateRequest0" type="text" value="0" disabled="disabled" /></td>
  </tr>
</table>
</form>

Your code has problems in 2 places - default value for GateRequest0 in JScript and in HTML. In JScript part, at line #6, you try to get the current value which could be changed to 0 by the time you read it again. As a result, the value will never be changed once you change it already one time. In HTML part, you specify the default value as 1600 which is also in the first display. As a result, you see the value as 1600 when the page is loaded.

From your original post, change 2 lines...

// this line #6 in your JScript
var var_gate_request_original = document.f1.GateRequest0.value
// to
var var_gate_request_original = 1600  // default value

// and this line #5 in your HTML
<input name="GateRequest0" type="text" value="1600" />
// to
<input name="GateRequest0" type="text" value="0" disabled />

// also you can delete line #14 in your JScript because it serves nothing.
// you could also merge the if condition for 'status' because it would be either true or false. Therefore, you could use if (status) {} else {} instead of 2 if statements.
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.