0

I have 2 input check boxes and a select list.

Whenever a checkbox is selected, a certain amount should be added into the element with "amount" as its id.

For example: The basic product price is 6000 and whenever a checkbox is selected, a certain amount is added to that basic product price.

This goes the same for select box which has the 10 input values. Each value is multiplied with 600 and the result is then added in the element with "amount" as id.

Check out the JSFiddle for the complete understanding: http://jsfiddle.net/0j796h33/13/

This is what I tried for the select box.

function ep(t)
{
    // addedprice = 0;
    var addedprice = t.value * 600;
    total = total + addedprice;
    // total = 6000 - total;
    document.getElementById('total').innerHTML = " (+) "+ addedprice;
    loginer = document.getElementById('login').checked;
    profer = document.getElementById('prof').checked;
    if(t.value > 0)
    {
        // total = total + addedprice;
        if(wpress)
        {
            wp(addedprice);
        }
        if(blogger)
        {
            blog(addedprice);
        }
        if(loginer)
        {
            login(addedprice);
        }
        if(profer)
        {
            prof(addedprice);
        }
    }

    document.getElementById('amount').value = 6000 + total;
    total = 6000 + total;
    document.getElementById('amount').value = total;
}

I need to implement this in Javascript.

2
  • 2
    why have you tagged this jQuery? do you want answers containing jQuery? Commented Dec 31, 2014 at 17:59
  • yes, answers would be preferred in jquery too Commented Dec 31, 2014 at 18:01

3 Answers 3

1

Please find an example of implementation here fiddle.

I've used jQuery as it is easier and needs less amount of code. I think it's more or less what you requested.

$("#login").change(function (ev) {
    var qty = +($("#amount").val());
    qty += $(this).prop("checked") ? 1500 : -1500;
    $("#amount").val(qty);
    updateAmountSpan(qty);
});

$("#prof").change(function (ev) {
    var qty = +($("#amount").val());
    qty += $(this).prop("checked") ? 2000 : -2000;
    $("#amount").val(qty);
    updateAmountSpan(qty);
});

var previous;
$("#ep").on('click', function () {
    previous = this.value;
}).change(function () {
    var qty = +($("#amount").val()),
        qty_sel = +($(this).val()) * 600,
        qty_prev = previous * 600;
    $("#amount").val(qty - qty_prev + qty_sel);
    updateAmountSpan(qty - qty_prev + qty_sel);
});

var updateAmountSpan = function (qty) {
    (qty > 0) ? $("#total").text("6000 (+) " + qty) : $("#total").text("6000");
};

Hope it helps and happy new year.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, That is the solution to it. Thanks a lot for the help. and a Happy new year to u too..!!
0

$(document).ready(function() {
    
    $("#login").change(function() {
    if(this.checked) {
        login();
    }
});

Try doing it like this and if you need add an else statement to remove if something has been previously added.

1 Comment

Check my complete implementation. It also update the value of amount as you click an option or a checkbox link to my implementation
0

This code will take every selected Item and multiplied it with 600 then add it to the total and the amount input :

var addedprice = 0;
var total = parseInt(document.getElementById("total").innerHTML);
var amount = document.getElementById("amount");
function ep()
{
  var sValue = parseInt($( "#ep" ).val()) * 600 ; 
  amount.value = sValue;
  total = total + sValue;
  document.getElementById("total").innerHTML = total;

}
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>

<input type=checkbox id="login" onchange="login()"> Item 1 <br/>
<input type=checkbox id="prof" onchange="prof()"> Item 2<br/>
    <select id="ep" onchange="ep()">
	    <option value="0">0</option>
		<option value="1">1</option>
		<option value="2">2</option>
		<option value="3">3</option>
		<option value="4">4</option>
		<option value="5">5</option>
		<option value="6">6</option>
		<option value="7">7</option>
		<option value="8">8</option>
		<option value="9">9</option>
		<option value="10">10</option>
	</select>Item 3 <br>
    
    <input type="text" id="amount"><br>
        <span id="total">6000 </span>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.