Skip to main content
Participant
May 21, 2008
Answered

Using Checkbox to enable/disable input text.

  • May 21, 2008
  • 12 replies
  • 3072 views
Can anyone help me with the Actionscript for having a checkbox when CHECKED it allows certain input text fields to be enabled, and when UNCHECKED they are disabled?

I am making a Shipping/Billing application and want it to have a checkbox so if they information is the same as the previous they can check the box and disable the fields.
This topic has been closed for replies.
Correct answer Ned Murphy
Sorry, I wasn't thinking, Try:

function clickHandler(event:MouseEvent):void {
inputtext.selectable = !event.target.selected;
}

Though it should have been selectable until you checked the checkbox since nothing set it false ahead of that.

12 replies

kglad
Community Expert
Community Expert
May 22, 2008
if you think that's better, you should check if a user can tab into your textfield.
May 22, 2008
inputtext.mouseEnabled=false;

seems better solution. Thanks Ned.
Ned Murphy
Legend
May 22, 2008
Since we posted within seconds of each other, you may have missed my alternative... If kGlad returns, then we'll hopefully see if his solution (changing it to a dynamic field) is the only safe solution for some reason.
May 22, 2008
Kglad is right. You can still enter.

So is it possiple to prevent this?

Ned Murphy
Legend
May 22, 2008
No need to reread, just a need to retest... and you're right--learning new stuff every day! I always assumed that no cursor meant no entry possible, so I never actually tried typing anything. Luckily, it's nothing I have to alert any clients about and change.

In that case, the following will work instead...

function clickHandler(event:MouseEvent):void {
inputtext.mouseEnabled = !event.target.selected;
}

If there's a problem with this approach, I'll be happy to learn that too.
kglad
Community Expert
Community Expert
May 22, 2008
again, assigning an input textfield's selectable property to false will NOT prevent text from being entered. it just prevents a user from selecting the text (that they may have entered).

re-read my above message.
Ned Murphy
Legend
May 22, 2008
You're welcome.
Ned Murphy
Ned MurphyCorrect answer
Legend
May 22, 2008
Sorry, I wasn't thinking, Try:

function clickHandler(event:MouseEvent):void {
inputtext.selectable = !event.target.selected;
}

Though it should have been selectable until you checked the checkbox since nothing set it false ahead of that.
TremmieAuthor
Participant
May 22, 2008
Wow that worked! Thank you! Can't believe my solution was just one tiny !.

Thanks!
Ned Murphy
Legend
May 22, 2008
Unless I've missed something somewhere (which isn't impossible), TextFields do not have an 'enabled' property. They have a 'selectable' property that you can set as true or false. Setting the selectable property false for an input text field effectively disables it.

If you can't select it, you can't enter anything into it. You can probably have actionscript do it, but then you can have that done for dynamic fields as well.

So your code should be:

inputtext.selectable = true;

function clickHandler(event:MouseEvent):void {
inputtext.selectable = event.target.selected;
}
TremmieAuthor
Participant
May 22, 2008
I appreciate the responses but I am still not getting the outcome I need. What your code does is makes the text field not selectable until the checkbox is selected, but I need the text field to be selectable ASLONG as the checkbox ISNT selected, and once the function is activated (eg. checking the box), the text field ISNT selectable until the box is unselected. Can anyone help with that?

Also I was considering using the TextInput Component since it does have a .enabled = true; syntax.
kglad
Community Expert
Community Expert
May 22, 2008
selectable won't work. you can still enter text in a textfield whose text you cannot select.
TremmieAuthor
Participant
May 22, 2008
Maybe I wasn't clear on what I am looking for, but I am creating a Shipping / Billing input form, and if the user's information is the same in the shipping as it is billing, I want them to check a box to disable the boxes.

I think it will be something like the attached code, however this code only allows the input text boxes to be ENABLED when the checkbox is checked, not DISABLED.