By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
463,156 Members | 688 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 463,156 IT Pros & Developers. It's quick & easy.

Why won't my function work?

P: 40
Hi all,

I've got a simple function I can't get to work. I've got two checkboxes that are associated with two textarea inputs. I disable the text areas initially. When the associated checkbox is clicked, I want it to enable that text area... and vice versa. Like a toggle.

My form is called pay_form. (The form actually spans several tables, in case that matters). Here's the tag for it:

Expand|Select|Wrap|Line Numbers
  1. <form ACTION="checkout3.php" method="POST" name="pay_form" id="pay_form" onSubmit="return chkPayForm();" style="margin:0;">
Here are the two checkboxes:

Expand|Select|Wrap|Line Numbers
  1. <input type="checkbox" name="commentbox" id="commentbox" value="0" onClick="msgBox('order_msg');" align="bottom"/>
  2.  
  3. <input type="checkbox" name="giftbox" id="giftbox" value="0" onClick="msgBox('gift_msg');" align="bottom"/>
And here are the two associated textareas:

Expand|Select|Wrap|Line Numbers
  1. <textarea name="order_msg" id="order_msg" cols="20" rows="3" maxlength="255" disabled></textarea>
  2.  
  3. <textarea name="gift_msg" id="gift_msg" cols="40" rows="3" maxlength="255" disabled></textarea>
Finally, here is my function:

Expand|Select|Wrap|Line Numbers
  1. function msgBox(element) {
  2.  var frm = document.pay_form;
  3.  if (frm.element.disabled == true) {
  4.     frm.element.disabled = false;
  5.     } else { frm.element.disabled = true; }
  6. }
Am I doing the parameter passing correctly? How come the textareas aren't toggling on and off? Please help! :) Thanks in advance.
Dec 14 '07 #1
Share this Question
Share on Google+
3 Replies

acoder
Expert Mod 15k+
P: 16,027
Try this instead:
Expand|Select|Wrap|Line Numbers
  1. function msgBox(element) {
  2.  var txtarea = document.getElementById(element);
  3.  if (txtarea.disabled == true) {
  4.     txtarea.disabled = false;
  5.     } else { txtarea.disabled = true; }
  6. }
Use the ID whenever you can to access elements.
Dec 14 '07 #2

P: 40
Hey that worked! Thanks!

Being a javascript newbie, I've never used getElementById. Gonna have to go check out more about it. Thanks again.
Dec 14 '07 #3

acoder
Expert Mod 15k+
P: 16,027
Glad it helped. Have a look at this link.
Dec 17 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.