View Full Version : Passing variable..

05-22-2006, 06:58 AM
I'm somewhat new to javascript, but not new to programming. I program in php and vb. What I'm trying to do and can't figure out is this. I'm calling a function in the onclick handler of a checkbox that I need to do two things (since I can't call two functions in one handler). When the function runs it checks to current count of checked checkboxes.

Now out beside the checkbox I have a disabled textbox that I'm trying to programmitcally enable when the checkbox is turned on, but I have several of these on one page so it's not as easy as just turning it on (so it seems anyway). So at the bottom of this function I'm trying to figure out how to pass the substr varable into a obj.form.textfield.disabled = false; string. my textfields are textfield1-xx and they match the checkbox they are next to, so if someone checks box number 4 it's named box4 and the textbox is textfield4, so by doing obj.name.substr(3) I get the number, and I can echo that all day long, I just can't put that into the obj.form call. How can I do that?

Thanks!! Jon

05-22-2006, 09:01 AM
there are a number of 'disable' scripts on

keyword search 'disable'

05-23-2006, 02:07 AM
I don't need a whole new script (unless this isn't possible), I just need to know how to pass a variable to the above referenced type of call. To elaborate a little more I'll provide the function and associated call so hopefully it's clear what I'm trying to do.


function countChoices(obj) {
max = 2; // max. number allowed at a time

box1 = obj.form.box1.checked;
box2 = obj.form.box2.checked;
box3 = obj.form.box3.checked;
box4 = obj.form.box4.checked;
box5 = obj.form.box5.checked;

count = (box1 ? 1 : 0) + (box2 ? 1 : 0) + (box3 ? 1 : 0) + (box4 ? 1 : 0) + (box5 ? 1 : 0);

if (count > max) {
alert("Oops! You can only choose up to " + max + " choices! \nUncheck an option if you want to pick another.");
obj.checked = false;

alert("obj.form.textfield" + obj.name.substr(3) + ".disabled = false;")
// End -->

The call I make in the checkbox is:

<input type=checkbox name=box1 onClick="countChoices(this)">

Next to it is this textfield:

<input disabled="true" name="textfield1" type="text" size="40">

What I want to happen is someone to check the box, within that function the textfield is passed to obj.form.textfield as above and enable the textbox. Is it possible like I'm trying to do it. I know it's possible in other languages, so I'd guess that it's possible in javascript I'm just not sure how.