Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trying to clear text field on checkbox unchecked

Status
Not open for further replies.

Pirellio

Programmer
Aug 25, 2008
18
US

Ok a person clicks on the check box the text field populates with a number 1

I want it to clear if they uncheck the text box now but can't

Here is the code - see function Quantity1()

<script language="javascript">
function OnlyNumbers1() {
document.theFood.qty1.value=filterNum(document.theFood.qty1.value)
function filterNum(str) {
re = /[^0-9\.]+/g;
// remove special characters like "$" and "," etc...
return str.replace(re, "");
}}
function Quantity1() {
if (document.theFood.qty1.checked) {
document.theFood.qty1.value=filterNum(document.theFood.qty1.value)
function filterNum(str) {
re = /[^A-Z\.]+/g;
// remove special characters like "$" and "," etc...
return str.replace(re, "");
} }
if (!document.theFood.qty1.checked) {
document.theFood.qty1.value = 1;
} }
</script>

<tr><td>Regular Box - $20.00</td><td> <input name="RegularBox" id="RegularBox" type="checkbox" value="RegularBox" onclick="Quantity1();" /> - QTY - <input name="qty1" id="qty1" type="text" maxlength="2" size="2" onkeyup="OnlyNumbers1();" /></td></tr>

 
As far as I see it from your code, you have the check box AND the text box with the same name. You can't do that...

Code:
<input type="checkbox" id="someCheckbox" name="someCheckbox" onclick="quantityOne();"/>
<input type="text" id="qty1" name="qty1" />

Code:
function filterNum(inStr){
 e = /[^A-Z\.]+/g;
 // remove special characters like "$" and "," etc...
 return inStr.replace(re, "");
}

function quantityOne() {
  var checkbox = document.getElementById("someCheckbox");
  var textBox = document.getElementById("qty1");

  if(checkbox.checked) {
    textBox.value = filtedNum(textBox.value);
  }else{
    textBox.value = "1";
  }
}
 
Thanks a lot, didn't see I was naming it the same
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top