3 Replies - 1969 Views - Last Post: 27 July 2009 - 10:18 AM

#1 crummydo  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 223
  • Joined: 06-January 09

Two functions with onClick

Post icon  Posted 26 July 2009 - 10:13 PM

Can I have two functions run when a button is clicked? I would like for the submit button to not only do its original function of calculating correct answers to a quiz, but to also set a cookie. If possible, how would it be written, in the onclick section in the html, or within the first function.

HTML
<input type=button onclick="Score()" value="Tally Score">


Javascript
...
	if (score <= 4) {
		window.location.href = "study.html";
	}
	
	if (score == 5) {
		 window.location.href = "youpassed.html";
		
	}


I was almost thinking something like if(score == 5) we would setCookie() && window.loc.... Would that work?

Is This A Good Question/Topic? 0
  • +

Replies To: Two functions with onClick

#2 BetaWar  Icon User is online

  • #include "soul.h"
  • member icon

Reputation: 1148
  • View blog
  • Posts: 7,145
  • Joined: 07-September 06

Re: Two functions with onClick

Posted 26 July 2009 - 10:17 PM

You could do either (call both functions, or add to the one that is already being called).

First off, cookies are set and such like so:
http://w3schools.com/js/js_cookies.asp

Now, if you wante to, you could do something like so (multiple function calles in your HTML onclick attribute):

<input type=button onclick="setCookie(); Score()" value="Tally Score">


Or (function call from within a function):

setCookie();
...
	if (score <= 4) {
		window.location.href = "study.html";
	}
	
	if (score == 5) {
		 window.location.href = "youpassed.html";
		
	}


Hopefully that makes some sense.
Was This Post Helpful? 0
  • +
  • -

#3 crummydo  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 223
  • Joined: 06-January 09

Re: Two functions with onClick

Posted 26 July 2009 - 10:58 PM

Let me put more code and more explination. If the user passes the quiz, a cookie needs to be set (so he doesnt have to take the quiz again and the page redirects to some url), and the user goes to special url. If not, the user goes to a "study.html" page, and no cookie is set. Oh, and that link was the first place I looked. I wouldn't have gotten as far as I have with my code with out the w3school

Here is my html
<html>
	<head>
		<title>Enlightenment Quiz</title>
		<link type="text/css" href="style.css" rel="stylesheet" />
		<script type="text/javascript" src="jquery.js">
		</script>
		<script type="text/javascript" src="ui.core.js">
		</script>
		<script type="text/javascript" src="quiz.js">
		</script>
		<script type="text/javascript">
			$(document).ready(function(){
				$("#accordion").accordion();
			});
		</script>
	</head>
	<body onload="checkCookie('passedquiz')">
		<table>
			<tr>
				<td>
					<img src="img/quizhead.png">

			   ... other misc code

						<tr>
							<td align=center>
								<!--<input type=button onclick="calculate()" value="Calculate Score"> &nbsp; &nbsp; -->
								
								<input type=button onclick="Score()" value="Get Results">
								<br/>
								<br/>
							</td>
						</tr>
					</form>
					</table>
				</body>
				</html>


und here is my JS
// The following is the quiz engine

var ans = new Array; //Setting the correct answer variable
var yourAns = new Array; //Setting the users answer variable
var score = 0; // all the correct answer to the questions in the HTML
ans[1] = "b";
ans[2] = "c";
ans[3] = "c";
ans[4] = "c";
ans[5] = "c";

// Scoring system to compare the input answer to the real answer
function scoreSys(question, answer){
	yourAns[question] = answer;
}

// Funciton to tally the score and lead to pass or fail pages
function Score(){
	var totalScore = 0;
	for (i = 1; i <= 5; i++) {
		totalScore = totalScore + i;
		if (ans[i] != yourAns[i]) {
			totalScore = totalScore + ans[i];
		}
		else {
			totalScore = totalScore;
			score++;
		}
	}
	
	if (score <= 4) {
	   window.location.href = "study.html";
	}
	
	if (score == 5) {
		window.location.href = "youpassed.html";
		
	}
}

// The following is the cookie script




function setCookie(c_name, value, expiredays){
	var exdate = new Date();
	exdate.setDate(exdate.getDate() + expiredays);
	document.cookie = c_name + "=" + escape(value) +
	((expiredays == null) ? "" : ";expires=" + exdate.toGMTString());
}

/* Old code
 
function calculate(){
	if (score == 5){
		setCookie('passedquiz', passed, 31);
	}
}
*/

function getCookie(c_name){
	if (document.cookie.length > 0) {
		c_start = document.cookie.indexOf(c_name + "=");
		if (c_start != -1) {
			c_start = c_start + c_name.length + 1;
			c_end = document.cookie.indexOf(";", c_start);
			if (c_end == -1) 
				c_end = document.cookie.length;
			return unescape(document.cookie.substring(c_start, c_end));
		}
	}
	return "";
}

function checkCookie()
{
passfail=getCookie('passedquiz');
if (passfail == passed)
  {
  window.location.href = "secretstore.html";
  }
} 

Was This Post Helpful? 0
  • +
  • -

#4 crummydo  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 223
  • Joined: 06-January 09

Re: Two functions with onClick

Posted 27 July 2009 - 10:18 AM

Does anybody know of any other tutorials besides the w3school's one. It was good, and it helped me start with the code, but my idea is different from prompting for a user name.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1