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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

auto populate textbox from checkbox value 1

Status
Not open for further replies.

smurf01

IS-IT--Management
Jul 6, 2002
470
GB
I wonder if anyone can help me please. I have created an htm page which has six checkboxes plus one text box within a form, i am trying to populate the text box when a certain checkbox is checked and i have modified some code that was given to me on this forum some time ago for another project however, i can get it to populate after i have checked the box and set the focus somewhere else but if I then uncheck the box I cannot get the text to disapear.

I have included the code below which i have set in the head

<script language=&quot;javascript&quot; type=&quot;text/javascript&quot;>
<!-- Hide script from old browsers

function showVal(inField){
thisField = eval(&quot;document.frmLabel.chk7&quot; + inField)
if (thisField.selectedIndex == 0){
eval(&quot;document.frmLabel.txtPallet&quot; + inField + &quot;.value = ''&quot;)
}
else{
eval(&quot;document.frmLabel.txtPallet&quot; + inField + &quot;.value = 'PALLET NEEDS SORTING'&quot;)
}
}

// end hiding script -->
</script>



And then I have this for the checkbox itself

<input name=&quot;chk7&quot; id=&quot;chk7&quot; type=&quot;checkbox&quot; class=&quot;bigcheck&quot; onChange=&quot;showVal('')&quot;>
<option value=&quot;&quot;></option>

Regards

Paul
 
Paul,

your script works for me in IE6. Make sure you are calling showVal with your select list's onChange event. It sounds like, from your description, you might be using onBlur.

Good luck.

--Dave
 
looking for info,
I think this is where i might be going wrong all i want is when the user ticks the checkbox then the text field is populated, not select from a list. Could this be the problem ? if so i am unsure how to solve it

Regards

Paul
 
That's it. You're making reference to your checkbox objects as if they were select list options.

Try this for the checkboxes:
Code:
<input type=checkbox name=whatever onClick=showVal(this,'a')>

...and change showVal to...

Code:
function showVal(cb, inField)
{
 if(!cb.checked)
 {
  eval(&quot;document.frmLabel.txtPallet&quot; + inField + &quot;.value = ''&quot;)
    }
    else{
        eval(&quot;document.frmLabel.txtPallet&quot; + inField + &quot;.value = 'PALLET NEEDS SORTING'&quot;)
    }
}

Here, I'm still assuming you're nameing your text field something like txtPallet with an 'a' or other uniquely identifying letter afterward. I'm also assuming you are interested in the box being empty if the checkbox is unchecked. Notice use of onClick instead of onBlur.

Try that out and let us know.

'hope that helps.

--Dave
 
Lookingfor info,
Thanks that has worked, i appreciate your help, how would i do it if I wanted the text to be there when the box was unchecked and cleared when the box was checked

Regards

Paul
 
Just change:

Code:
if(!cb.checked)

...to:

Code:
if(cb.checked)

'checked' is a boolean attribute of 'cb' and is true if the checkbox is checked and false if the checkbox is not. The exclamation point in front reverses it. In other words, if cb is checked, cb.checked is true and !cb.checked is false.

'probably more than you needed! :) Have a great day...

--Dave
 
Dave,
Thanks for your help. It works a treat, A star for You

Regards

Paul
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top