jeudi 19 mars 2015

Cannot update score

I was trying to make a content score as a class assignment.


Assume : (The user sees a URL and then select the checkboxes that are assigned to issues . Each issue is assigned a score in a array.)


Whats working :

Individual checks are registered with their respective scores being displayed


Whats not working :

Can someone help me to update the score ( as the user checks the checkbox or unchecks).


I am assuming in future if i want to increase the issues I will be able to do that since it is in an array. (am i right)


(my week 4 in JS)





//Set up an array with the respective score
var code = new Array();
code["v1"] = 1;
code["v2"] = 2;
code["v3"] = 3;
code["v4"] = 5;




// As the user selects the checkbox I want to keep on adding the score and as the user unchecks I want to recalculate and display.

function getvalueScore() {
var score = 0;

//Get a reference to the form
var theForm = document.forms["contentForm"];

//Get a reference to the score from the "ContentForm"
var contentScore = theForm.elements["contentScore"];


// loop through each check box
for (var i = 0; i < contentScore.length; i++) {
//if the radio button is checked
if (contentScore[i].checked) {
// I want to calculate and keep updating the score
score = code[contentScore[i].value];

}
}
//return score
return score;
}


function calculateTotal() {
//calculation for final score
var scoreCard = getvalueScore();

//display the result
var divobj = document.getElementById('totalPrice');
divobj.style.display = 'block';
divobj.innerHTML = "Your Content Score is " + scoreCard;

}

function hideTotal() {
var divobj = document.getElementById('totalPrice');
divobj.style.display = 'none';
}



<!DOCTYPE html>
<html>

<head>
<title>Content Score</title>
<meta charset="utf-8">


<script src="formcalculations.js"></script>

</head>

<body onload='hideTotal()'>
<div id="wrap">
<form action="" id="contentForm" onsubmit="return false;">
<div>
<div class="cont_order">

Content Score</br>
<label>Please select the issues you see on the page to calculate the content score</label>
</br>

<label class='radiolabel'>
<input type="checkbox" name="contentScore" value="v1" onclick="calculateTotal()" />No content value</label>
<br/>

<label class='radiolabel'>
<input type="checkbox" name="contentScore" value="v2" onclick="calculateTotal()" />Mediocre content value</label>
<br/>

<label class='radiolabel'>
<input type="checkbox" name="contentScore" value="v3" onclick="calculateTotal()" />Obsolete content</label>
<br/>

<label class='radiolabel'>
<input type="checkbox" name="contentScore" value="v4" onclick="calculateTotal()" />Irrelevant content</label>
<br/>


<br/>

<br/>


<div id="totalPrice"></div>


</div>



</div>
</form>
</div>
<!--End of wrap-->

</body>

</html>



Aucun commentaire:

Enregistrer un commentaire