Forum Moderators: open

Message Too Old, No Replies

JavaScript checkbox help

         

andrewsmd

4:48 pm on Sep 18, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ok so I have this form.
<form ENCTYPE="multipart/form-data" name = "form" method = "post">
and a text box with a check box next to it that looks something like this
________________________
¦________________________¦
_
¦_¦

Here is the code for the textbox
<INPUT NAME="containText" TYPE="text" size = "32" onfocus="checker('contain')" value = "<?php echo($_SESSION['containText']); ?>">

Here is the code for the check box next to it
<input type = "checkbox" name = "contain" <?php echo($_SESSION['contain']); ?>>

Now I use those PHP session echos to keep the values
i.e. if they enter something into the texbox containText and then submit, I store the value in that textbox to $_SESSION['containText'] so it will be in there after the PHP does its work. The same is for the checkbox, basically if it is checked then I set $_SESSION['contain'] = "checked"; so it is checked when the page reloads.
I want to use JavaScript to automatically check the box when the user clicks in the textbox. Meaning if they start to type text in the box the checkbox automatically gets checked because it needs to be checked to use that text in my form. Here is the JavaScript function I have tried to do but I am not a JS programmer so bear with me.
//this function checks the corresponding checkbox
//on a focus of the textbox it goes with
function checker(checkbox) {

var checkBox=document.getElementByName(checkbox);
checkBox.value = "checked";

}//checker
Now it won't check the checkbox when I click on the textbox. Is it because I echo PHP for the value. If so, I can take out the checkbox php echo but could someone show me how to keep the value of the checkbox then with JS. Thanks,

Trace

5:22 pm on Sep 18, 2008 (gmt 0)

10+ Year Member



Using what you have supplied;
//this function checks the corresponding checkbox
//on a focus of the textbox it goes with
function checker(checkbox) {

var checkBox=document.form.contain;
checkBox.checked = true;

}//checker


That should work.

There is no method called getElementByName and setting a checkbox to checked is done like this: checked = true

andrewsmd

6:33 pm on Sep 18, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That works but only on the checkbox contain. I want to be able to pass in the checkbox name so I can just call the function. i.e.
checker('check') will check the box named check
checker('check2') will check the box named check2 something like this

<input type = "text" name = "text" onfocus = "checker('check')">
<input type = "checkbox" name = "check">

<input type = "text" name = "text2" onfocus = "checker('check2')">
<input type = "checkbox" name = "check2">
I want to be able to pass in that variable and put it where you have
document.form.conatin but replacing contain with a passed in variable. thanks,

andrewsmd

6:35 pm on Sep 18, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here is how I would want the method to work
function checker(checkbox) {

var checkBox=document.form.checkbox;
checkBox.checked = true;

}//checker
but checkbox is the passed in variable i.e. 'check' or 'check2'

Trace

7:10 pm on Sep 18, 2008 (gmt 0)

10+ Year Member



My bad, sorry.
//this function checks the corresponding checkbox
//on a focus of the textbox it goes with
function checker(checkbox) {

var checkBox=document.form[checkbox];
checkBox.checked = true;

}//checker

edit:
I just tested it and it seems to work, but it feels like a hack. Personally I would be using IDs on my form elements and affecting them with getElementById method.

[edited by: Trace at 7:14 pm (utc) on Sep. 18, 2008]

andrewsmd

7:42 pm on Sep 18, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What is better about the id method? I have seen it but before like I said I am not a JavaScript programmer. Give me a PHP server however, and I can create magic.

andrewsmd

7:43 pm on Sep 18, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



And just and FYI that worked thanks.

Trace

8:27 pm on Sep 18, 2008 (gmt 0)

10+ Year Member



Glad you got it working.

Not sure if it's right or wrong, maybe it's just "force of habit". To me, IDs which are unique seem less troublesome. Like, what would happen if you mistakenly made two check boxes with the same name?

Maybe one of the more knowledgeable WebmasterWorld'ers can answer that.

andrewsmd

8:42 pm on Sep 18, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well couldn't you also give a checkbox id the same name twice? I would assume if you give it the same name there are three things that would happen.
1. it would not do anything to either (not likely)
2. it would do it to both of them. (thats my guess)
3. it would either do it to the first or last no matter how many had the same name.

andrewsmd

8:47 pm on Sep 18, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I checked and it just works on the last checkbox that you name that. However, it is a bad practice to use the same name for any two different variables anyways. Thanks for all of your help