Answers To Questions About JavaScript
At Willmaster.com we receive lots of questions. Here are a few about JavaScript with the answer they were responded to.
Here are the answers to some JavaScript questions we have received.
Question:
If I am validating a form AND preventing multiple clicks, is the following code correct?
<input type="text" onClick="return count()" onClick="ValidateAll()">
Thanks. Russell
Answer:
Only one onclick attribute may be used.
The use of several onclick attributes can be simulated by including one of the onclick values in the function of the other.
In your example, the count() function could include the line:
ValidateAll();
For example:
function count() { ValidateAll(); // function count() line // function count() line // function count() line }
Then only the
onClick="return count()"
attribute is specified in the INPUT field:
<input type="text" onClick="return count()">
Question:
The tutorial in Possibilities Issue # 80: "Putting Form Confirmation Pages Into Popups" is great. When using radio buttons and checkboxes, the form works okay but the JavaScript line
constructURL('content11',document.myform.content11.value);
sends the value of the field whether or not the field was checked. How can I have the value of the field sent only when it's been checked? Wilhelm
Answer:
As you noticed, radio buttons and checkboxes need somewhat different handling to retrieve their values. Each radio button and checkbox must first be tested to see whether or not it is checked. Examples:
if(document.FormName.CheckName.checked == true) { constructURL('CheckName',document.FormName.CheckName.value); } if(document.FormName.RadioName[0].checked == true) { constructURL('RadioName',document.FormName.RadioName.value); } if(document.FormName.RadioName[1].checked == true) { constructURL('RadioName',document.FormName.RadioName.value); }
Replace every occurrance of CheckName with the checkbox field name and every occurrance of RadioName with your radio button field names. Also, replace every occurrance of FormName with the name you gave your form.
Question:
How do I use radio-buttons to activate selective fields on a form using perl/cgi?
For example, if there are two choices with a submit -- no and yes. The "yes" option is associated with a text input field and "no" has nothing associated to it. If I select "no", it should deactivate the text box and not allow the user to type into it. If I select "yes" the text box should be active to take input. Srinivas
Answer:
This is probably best handled with JavaScript. Whenever a form field becomes "in focus" or is "blurred," or when the submit button is clicked, your JavaScript can check which radio button is checked and, if appropriate, blank the text field.
The reason CGI is not suitable is because the form must be submitted before the program can do anything with it. What you need, in this case, is something that will do the checking before the form is submitted. JavaScript is good for that.
Will Bontrager