vozzek Posted December 14, 2007 Share Posted December 14, 2007 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. Quote Link to comment Share on other sites More sharing options...
emehrkay Posted December 14, 2007 Share Posted December 14, 2007 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 Quote Link to comment Share on other sites More sharing options...
fenway Posted December 14, 2007 Share Posted December 14, 2007 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. Quote Link to comment Share on other sites More sharing options...
emehrkay Posted December 14, 2007 Share Posted December 14, 2007 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.