/    Sign up×
Community /Pin to ProfileBookmark

How to reference all the forms in one function?

Hallo!

Thanks to JMRKER ? I have this function below which will show a ‘secret’ message from the hidden paragraph after the user answers [U]all the questions in the form[/U]. (!no matter the choice!).

But what if I had many similar ‘radio’ forms – each of which with the same ‘yes’ or ‘no’ radio buttons but with different amount of questions (- only one form for each page) ??? –

[B]How to reference all the forms in one function?[/B]

I reapeat: It doesn’t matter what the choice was. The user should only answer all of the questions to see the hidden message!

[CODE]<!DOC HTML>
<html>
<head>
<title> Untitled </title>
<script type=”text/javascript”>
// From: http://www.webdeveloper.com/forum/showthread.php?t=255233

function getRBtnName(GrpName) {
var sel = document.getElementsByName(GrpName);
var fnd = -1;
var str = ”;
for (var i=0; i<sel.length; i++) {
if (sel[i].checked == true) { str = sel[i].value; fnd = i; }
}
// return fnd; // return option index of selection
// comment out next line if option index used in line above
return str;
}

function show_info() {
var flag = true;
if (getRBtnName(‘one’) == ”) { flag = false; }
if (getRBtnName(‘two’) == ”) { flag = false; }
if (getRBtnName(‘three’) == ”) { flag = false; }
if (getRBtnName(‘four’) == ”) { flag = false; }

var paragraph = document.getElementById(‘info’);
if (flag == false) {
alert(‘You must answer all the questions!!!’);
paragraph.style.display = “none”;
return false; } // change this to true after testing script
else {
paragraph.style.display = “block”;
return false;
}
}
</script>

</head>
<body>
<form name=”smallform” onsubmit=”return show_info()”>
<div><br>

<table>
<tr>
<td>1</td>
<td>First question?</td>
<td><input type=”radio” name=”one” value=”Yes” />Yes</td>
<td><input type=”radio” name=”one” value=”No” />No</td>
</tr>

<tr>
<td>2</td>
<td>Second question?</td>
<td><input type=”radio” name=”two” value=”Yes” />Yes</td>
<td><input type=”radio” name=”two” value=”No” />No</td>
</tr>

<tr>
<td>3</td>
<td>Third question?</td>
<td><input type=”radio” name=”three” value=”Yes” />Yes</td>
<td><input type=”radio” name=”three” value=”No” />No</td>
</tr>

<tr>
<td>4</td>
<td>Fourth question?</td>
<td><input type=”radio” name=”four” value=”Yes” />Yes</td>
<td><input type=”radio” name=”four” value=”No” />No</td>
</tr>
</table>

</div>
<div><br>
<input type=”button” name=”diagnosis” onclick=”show_info();” value=”Diagnosis”>
<input type=”reset” value=”Clear data”
onclick=”document.getElementById(‘info’).style.display=’none'”>
</div>

</form>

<p id=”info” style=”display:none;background-Color:red;color:white;width:200px;font-size:2em”>
Here is your reward for answering all the questions!
</p>

</body>
</html>[/CODE]

Thanks in advance for help ?

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@WolfShadeJan 13.2012 — I believe it already is set for use with more than one form. You can drop the javascript into an external file and include it on every page that has a similar form; it should still work.
Copy linkTweet thisAlerts:
@KovaliciousauthorJan 13.2012 — It works if I only have 4-questions-form but when the amount of questions in other form is different, it doesn't work.

What to do instead of writing 'if' statement for each question (...and a new function for each form with different amount of radio groups) ???
Copy linkTweet thisAlerts:
@xelawhoJan 13.2012 — see if this suits your needs...

[CODE]
<!DOC HTML>
<html>
<head>
<title> Untitled </title>
<script type="text/javascript">
// From: http://www.webdeveloper.com/forum/showthread.php?t=255233

var names=[];

function getRBtnName(GrpName) {
var sel = document.getElementsByName(GrpName);
var fnd = -1;
var str = '';
for (var i=0; i<sel.length; i++) {
if (sel[i].checked == true) { str = sel[i].value; fnd = i; }
}
// return fnd; // return option index of selection
// comment out next line if option index used in line above

return str;
}

function show_info(theform) {
names.length=0;
var flag = true;
var inps = theform.getElementsByTagName("input");
for (var i=0; i<inps.length; i++) {
if (inps[i].type=="radio") {
names.push(inps[i].name)
if(inps[i].name==names[names.length-2]){ names.pop();}
}
}
for (var a=0; a<names.length; a++) {
if (getRBtnName(names[a]) == '') { flag = false; }
}

var paragraph = document.getElementById('info');
if (flag == false) {
alert('You must answer all the questions!!!');
paragraph.style.display = "none";
return false; } // change this to true after testing script
else {
paragraph.style.display = "block";
return false;
}
}
</script>

</head>
<body>
<form name="smallform" onsubmit="return show_info()">
<div><br>

<table>
<tr>
<td>1</td>
<td>First question?</td>
<td><input type="radio" name="one" value="Yes" />Yes</td>
<td><input type="radio" name="one" value="No" />No</td>
</tr>

<tr>
<td>2</td>
<td>Second question?</td>
<td><input type="radio" name="two" value="Yes" />Yes</td>
<td><input type="radio" name="two" value="No" />No</td>
</tr>

<tr>
<td>3</td>
<td>Third question?</td>
<td><input type="radio" name="three" value="Yes" />Yes</td>
<td><input type="radio" name="three" value="No" />No</td>
</tr>

<tr>
<td>4</td>
<td>Fourth question?</td>
<td><input type="radio" name="four" value="Yes" />Yes</td>
<td><input type="radio" name="four" value="No" />No</td>
</tr>
</table>

</div>
<div><br>
<input type="button" name="diagnosis" onclick="show_info(this.form);" value="Diagnosis">
<input type="reset" value="Clear data"
onclick="document.getElementById('info').style.display='none'">
</div>

</form>

<p id="info" style="display:none;background-Color:red;color:white;width:200px;font-size:2em">
Here is your reward for answering all the questions!
</p>

</body>
</html>

[/CODE]
Copy linkTweet thisAlerts:
@KovaliciousauthorJan 13.2012 — Yes, it works with all my 'radio' forms! ?

Thanks a lot!!!
Copy linkTweet thisAlerts:
@xelawhoJan 13.2012 — you're welcome. If you would like to tell the user which question they didn't answer, you can change that loop at the start of the show_info function to be like this:

[CODE]
for (var a=0; a<names.length; a++) {
if (getRBtnName(names[a]) == '') {
alert("you haven't answered question "+names[a]);
flag = false; }
}[/CODE]
×

Success!

Help @Kovalicious spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...