JavaScript experts or jQuery need help here.

Discussion in 'JavaScript' started by Magina™, Oct 25, 2011.

  1. #1
    Greetings!

    I'm just a new student in javascript. I have an assignment to add the following values but I don't know how.

    Let me show you first my code.

    JS

    You noticed that there are two select option tag in the html file that is an array. Can you help me how to add the two tags whenever I click the calculate button using JS or jQuery?

    I tried my best to do it but no luck. It just gives me 'NaN' alert return when ever I click the calculate button.

    :)

    I hope you can help me guys.
     
    Magina™, Oct 25, 2011 IP
  2. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #2
    Are you required to use jQuery here? (You're shooting a flea with an atomic bomb.) Javascript itself is powerful enough to add 2 numbers. Use something of the form of var discount = parseInt($(".discount").val()); if you have to use jQuery. "15" isn't a number, it's characters. $(".discount").val() is the characters in the thing with the class discount, not the numeric value.
     
    Rukbat, Oct 25, 2011 IP
  3. AsHinE

    AsHinE Well-Known Member

    Messages:
    240
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    138
    #3
    try this
    
    <script type="text/javascript">
    $(document).ready(function(){
    
    $("#calc").click(function(){ 
    	var total = 0;
    	$(".discount\\[\\]").each(function(){
    		total += $(this).val()*1;
    	});
    	alert(total);
    });
    });
    </script>
    
    HTML:
     
    AsHinE, Nov 3, 2011 IP