Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
How can I auto-check a checkbox when a corresponing textfield is edited?
For example, when textfield1 is clicked on or edited, checkbox1 will automatically "tick".
Anyone know of a link or tutorial on how to do so? I've googled it, though the closest I could find was the "check-all/un-check all" method.
Thanks

if they're in the same form, add this on the text field:
<input onfocus="this.form.THECHECKBOXNAME.checked=true" type="text" name="etc...

Thanks for your help.
I gave it a try, though it doesn't seem to work. I'm getting the error:
'this.form.checkbox.225' is null or not an object
I guess I should have mentioned that it's an array of checkboxes, each with a unique identifier given to it by PHP code.
The checkbox is formed like:
<input name="checkbox[<?php echo $row['id'];?>]" type="checkbox"...
My input field code with your javascript:
<input onfocus="this.form.checkbox[<?php echo $row['id'];?>].checked=true" type="text" name="e_price[<? echo $row['id'];?>]...
Looking at the HTML (below), it looks like I'm getting the right results, though for some reason it won't work. Any ideas?
<td><div align="center"><input name="checkbox[225]" type="checkbox" value="225"></div></td>
<td><input onfocus="this.form.checkbox[225].checked=true" type="text" name="sym[225]" value="AAPL"></td>
<td><input onfocus="this.form.checkbox[225].checked=true" onclick="ds_sh(this);" name="e_date[225]" value="2007-05-22" readonly="readonly" style="cursor: text" /></td>
<td width="125"><input onfocus="this.form.checkbox[225].checked=true" type="text" name="e_price[225]" value="85.45"></td>

Hi,
I'm not sure on how to refer (in JavaScript) to a field name with box brackets. It might be possible, but maybe not. Can somebody confirm?
But if I need to create something like yours, I'll refer to the field ID, or by using those "JavaScript object thingy" :D Like "on focus, look at parent node's first checkbox, then mark it as checked".
Anyways, take a look here, view the source, and the PHP source.
It's the first example (referring to the checkbox ID).
The wrapped JavaScript function - checkTheBox(boxID) - is used to minimize potential headaches when doing these things in a not-so-simple applications.

Thanks.
It's weird, I tried your examples and I received the desired results (by changing the parenthesis to brackets in the jscript function), in the HTML code at least, though it still doesn't auto-check. Oh well.

> by changing the parenthesis to brackets in the jscript functioncan't do that. it must read like this:
getElementByID(ID_of_the_object)
Maybe you're confusing name and ID?
in my example, the checkbox field name is "checkbox[1]" and the ID is "c1", and so on.
'c' + boxID
will be parsed as "c1", and so on...Forget the first part of my previous message. I read it again, and I'm confused -_-
Just check and try to recreate the sample. I think it's quite similar with your apps, including your PHP.

You're a genious! It works. I overlooked the little "c" in front of the checkbox ID.
It works like a gem - thanks for your help.

![]() |
PHP/MYSQL updating w/chec...
|
Is this possible?
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |