Why won't my function work?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vozzek
    New Member
    • Sep 2007
    • 40

    #1

    Why won't my function work?

    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:

    Code:
    <form ACTION="checkout3.php" method="POST" name="pay_form" id="pay_form" onSubmit="return chkPayForm();" style="margin:0;">
    Here are the two checkboxes:

    Code:
    <input type="checkbox" name="commentbox" id="commentbox" value="0" onClick="msgBox('order_msg');" align="bottom"/>
    
    <input type="checkbox" name="giftbox" id="giftbox" value="0" onClick="msgBox('gift_msg');" align="bottom"/>
    And here are the two associated textareas:

    Code:
    <textarea name="order_msg" id="order_msg" cols="20" rows="3" maxlength="255" disabled></textarea>
    
    <textarea name="gift_msg" id="gift_msg" cols="40" rows="3" maxlength="255" disabled></textarea>
    Finally, here is my function:

    Code:
    function msgBox(element) {
     var frm = document.pay_form;
     if (frm.element.disabled == true) {
        frm.element.disabled = false;
    	} else { frm.element.disabled = true; }
    }
    Am I doing the parameter passing correctly? How come the textareas aren't toggling on and off? Please help! :) Thanks in advance.
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Try this instead:[code=javascript]function msgBox(element) {
    var txtarea = document.getEle mentById(elemen t);
    if (txtarea.disabl ed == true) {
    txtarea.disable d = false;
    } else { txtarea.disable d = true; }
    }[/code]Use the ID whenever you can to access elements.

    Comment

    • vozzek
      New Member
      • Sep 2007
      • 40

      #3
      Hey that worked! Thanks!

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

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        Glad it helped. Have a look at this link.

        Comment

        Working...