Jump to content

Why won't my function work?


vozzek

Recommended Posts

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:

 

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

 

Here are the two checkboxes:

 

<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:

 

<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:

 

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.

Link to comment
Share on other sites

function msgBox(element){

var ta_ele = document.getElementById(element);

ta_ele.disabled = (ta_ele.disabled) ? false : true;

}

 

you are tyring to get to the text area through the forms array, while that is possible, it is unnecessary. You can directly access the text area as you have given them unique ids

Link to comment
Share on other sites

you are tyring to get to the text area through the forms array, while that is possible, it is unnecessary. You can directly access the text area as you have given them unique ids

I disagree -- the DOM reference has already been looked up for you in the form's element collection! Why look it up again? That's slower.

Link to comment
Share on other sites

The negligible performance hit is greatly outweighed by the decrease in margin of error/ time spent debugging.

 

Is there really a time difference because all document.getElementById() is doing is scanning the dom and stopping on the first found id? I would assume that dom node tree would have to be HUGE to see difference. And if it is that big, any action would be costly.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.