Home > Technical & Creative Skills > Programming, Servers & Scripts

Associating a value to a button. (4)


08-20-2015 01:12 AM #1 kacperp (Member)
Associating a value to a button.

Hey,
I'm trying to associate a numerical value to a button in a quiz, so I can later sum it up and call out.
Let's say that the quiz has 3 questions and ach of the questions has two options: A and B.
I want to associate the following values to each of the options:
1A = 10
1B = 15
2A = 5
2B = 25
3A = 20
3B = 30

Let's say that user clicked the following buttons 1A, 2A, 3A. Now I want to call out the sum of the values for the following options. In this case 35.

Does anybody know how can I achieve this using just HTML+CSS and JS?

Any help is greatly appreciated.


08-20-2015 03:34 AM #2 imphocused (Member)

this is VERY basic, but can provide you a starting point

HTML Code:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
var x=0;
function pushmybutton(id,value){
	x+=value;
	document.getElementById(id).disabled = true;
};
</script>
</head>

<button id='B1A' onclick=pushmybutton('B1A',10)>A</button>
<button id='B1B' onclick=pushmybutton('B1B',15)>B</button>
<br/>
<button id='B2A' onclick=pushmybutton('B2A',5)>A</button>
<button id='B2B' onclick=pushmybutton('B2B',25)>B</button>
<br/>
<button id='B3A' onclick=pushmybutton('B3A',20)>A</button>
<button id='B3B' onclick=pushmybutton('B3B',30)>B</button>
<br/>

<button onclick=alert(x);>Results</button>
</html>


08-20-2015 04:13 PM #3 kacperp (Member)

Thanks for the response.
If I want to associate another onclick attribute to the button, should I do it in a following way?
onclick="pushmybutton('B3B',30);ga('send', 'event', 'Q4', 'AppNo');"


08-28-2015 09:12 AM #4 imphocused (Member)

Sorry, been missing notifications. Yes, you can do that, you'll just need to create the ga(var1,var2,var3,var4) function the same way as the pushmybutton(var1,var2) function


Home > Technical & Creative Skills > Programming, Servers & Scripts