Computing.Net > Forums > Web Development > Auto-check a check box on edit

Auto-check a check box on edit

Reply to Message Icon

Original Message
Name: jb60606
Date: July 2, 2007 at 11:58:14 Pacific
Subject: Auto-check a check box on edit
OS: Windows XP
CPU/Ram: 2.8Ghz/512MB
Model/Manufacturer: Homeade
Comment:

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


Report Offensive Message For Removal

Response Number 1
Name: Laler
Date: July 2, 2007 at 16:24:50 Pacific
Subject: Auto-check a check box on edit
Reply: (edit)

if they're in the same form, add this on the text field:

<input onfocus="this.form.THECHECKBOXNAME.checked=true" type="text" name="etc...

---
JakartaSpot.com


Report Offensive Follow Up For Removal

Response Number 2
Name: jb60606
Date: July 2, 2007 at 17:40:11 Pacific
Subject: Auto-check a check box on edit
Reply: (edit)

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>



Report Offensive Follow Up For Removal

Response Number 3
Name: Laler
Date: July 3, 2007 at 02:12:29 Pacific
Subject: Auto-check a check box on edit
Reply: (edit)

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.

---
JakartaSpot.com


Report Offensive Follow Up For Removal

Response Number 4
Name: jb60606
Date: July 3, 2007 at 13:52:44 Pacific
Subject: Auto-check a check box on edit
Reply: (edit)

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.


Report Offensive Follow Up For Removal

Response Number 5
Name: Laler
Date: July 3, 2007 at 15:09:42 Pacific
Subject: Auto-check a check box on edit
Reply: (edit)


> by changing the parenthesis to brackets in the jscript function

can'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.

---
JakartaSpot.com


Report Offensive Follow Up For Removal


Response Number 6
Name: jb60606
Date: July 3, 2007 at 22:52:11 Pacific
Subject: Auto-check a check box on edit
Reply: (edit)

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.


Report Offensive Follow Up For Removal






Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Auto-check a check box on edit

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software