Forum Moderators: open
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,
//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
There is no method called getElementByName and setting a checkbox to checked is done like this: checked = true
<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,
//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]