lundi 20 novembre 2017

JavaScript set default and change price with checkboxes

I am trying to create an interface of a website which has a varying price dependent on whether check boxes are checked and which has a default value if none are selected.

The varying value is in a table which has the id 'Level1Price' which I want to default to a value of '£5.11' if neither of the two check boxes are checked and the value to chage if either one or both are selected and where the two chekc boxes on their own would hold a different value each.

The two check boxes have the id's 'partner' and 'children'. When no checkboxes are checked (for the purpose of this demonstartion) the value of 'Level1Price in the table should be 5.

If just the 'partner' checkbox is checked the value of 'Level1Price' is 10.

If just the 'children' checkbox is checked the value of 'Level1Price' is 12.

If both checkboxes are checked the value of 'Level1Price' is 20.

var partner = document.getElementById("partner");
var children = document.getElementById("children");
function calc()

if (!partner.checked && !children.checked) 

{
  document.getElementById('Level1Price')element.innerHTML = 5;


} else if  (partner.checked && !children.checked) 

{

document.getElementById('Level1Price')element.innerHTML = 10;


} else if (!partner.checked && children.checked) 

{     
  document.getElementById('Level1Price')element.innerHTML = 12;


} else if  (partner.checked && children.checked)

 {

  document.getElementById('Level1Price')element.innerHTML = 20;

 } 

This is the code that I thought would work and i'm struggling. I apologies if I have made rookie mistakes i'm quite new to this and couldn't find any working resolutions anywhere.

Thanks in advance.




Aucun commentaire:

Enregistrer un commentaire