Multiply input values into span












1















Here is my fiddle:



http://jsfiddle.net/19gwk4q6/



Html:



<ul>
<li id="1">
<input type="text" name="1" class="parent" />
<input type="hidden" name="rank" value="8" class="rank" />
<span class="result"></span>
</li>
<li id="2">
<input type="text" name="2" class="parent" />
<input type="hidden" name="rank" value="1.75" class="rank" />
<span class="result"></span>
</li>
<li id="3">
<input type="text" name="1" class="parent" />
<input type="hidden" name="rank" value="10" class="rank" />
<span class="result"></span>
</li>
</ul>


JS:



$(document).on("change", ".parent", function() {
var sum = 0;
$(".parent").each(function() {
var rank = $("input").next(".rank").val();
sum = $(this).val() * rank;
});
$("input").next(".result").val(sum);
});


I have server side generated list and I need to multiply input values .parent * .rank and get result into span.result in each <li>. I was trying to use .closest () and .next() functions, but it is not working.



Thank you for help!










share|improve this question























  • in tag span you must use function text set value for it. $("input").next(".result").val(sum); ---> $("input").next(".result").text(sum);

    – son pham
    Nov 23 '18 at 8:22
















1















Here is my fiddle:



http://jsfiddle.net/19gwk4q6/



Html:



<ul>
<li id="1">
<input type="text" name="1" class="parent" />
<input type="hidden" name="rank" value="8" class="rank" />
<span class="result"></span>
</li>
<li id="2">
<input type="text" name="2" class="parent" />
<input type="hidden" name="rank" value="1.75" class="rank" />
<span class="result"></span>
</li>
<li id="3">
<input type="text" name="1" class="parent" />
<input type="hidden" name="rank" value="10" class="rank" />
<span class="result"></span>
</li>
</ul>


JS:



$(document).on("change", ".parent", function() {
var sum = 0;
$(".parent").each(function() {
var rank = $("input").next(".rank").val();
sum = $(this).val() * rank;
});
$("input").next(".result").val(sum);
});


I have server side generated list and I need to multiply input values .parent * .rank and get result into span.result in each <li>. I was trying to use .closest () and .next() functions, but it is not working.



Thank you for help!










share|improve this question























  • in tag span you must use function text set value for it. $("input").next(".result").val(sum); ---> $("input").next(".result").text(sum);

    – son pham
    Nov 23 '18 at 8:22














1












1








1


0






Here is my fiddle:



http://jsfiddle.net/19gwk4q6/



Html:



<ul>
<li id="1">
<input type="text" name="1" class="parent" />
<input type="hidden" name="rank" value="8" class="rank" />
<span class="result"></span>
</li>
<li id="2">
<input type="text" name="2" class="parent" />
<input type="hidden" name="rank" value="1.75" class="rank" />
<span class="result"></span>
</li>
<li id="3">
<input type="text" name="1" class="parent" />
<input type="hidden" name="rank" value="10" class="rank" />
<span class="result"></span>
</li>
</ul>


JS:



$(document).on("change", ".parent", function() {
var sum = 0;
$(".parent").each(function() {
var rank = $("input").next(".rank").val();
sum = $(this).val() * rank;
});
$("input").next(".result").val(sum);
});


I have server side generated list and I need to multiply input values .parent * .rank and get result into span.result in each <li>. I was trying to use .closest () and .next() functions, but it is not working.



Thank you for help!










share|improve this question














Here is my fiddle:



http://jsfiddle.net/19gwk4q6/



Html:



<ul>
<li id="1">
<input type="text" name="1" class="parent" />
<input type="hidden" name="rank" value="8" class="rank" />
<span class="result"></span>
</li>
<li id="2">
<input type="text" name="2" class="parent" />
<input type="hidden" name="rank" value="1.75" class="rank" />
<span class="result"></span>
</li>
<li id="3">
<input type="text" name="1" class="parent" />
<input type="hidden" name="rank" value="10" class="rank" />
<span class="result"></span>
</li>
</ul>


JS:



$(document).on("change", ".parent", function() {
var sum = 0;
$(".parent").each(function() {
var rank = $("input").next(".rank").val();
sum = $(this).val() * rank;
});
$("input").next(".result").val(sum);
});


I have server side generated list and I need to multiply input values .parent * .rank and get result into span.result in each <li>. I was trying to use .closest () and .next() functions, but it is not working.



Thank you for help!







jquery next closest multiplying






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 23 '18 at 8:10









Johann GJohann G

233




233













  • in tag span you must use function text set value for it. $("input").next(".result").val(sum); ---> $("input").next(".result").text(sum);

    – son pham
    Nov 23 '18 at 8:22



















  • in tag span you must use function text set value for it. $("input").next(".result").val(sum); ---> $("input").next(".result").text(sum);

    – son pham
    Nov 23 '18 at 8:22

















in tag span you must use function text set value for it. $("input").next(".result").val(sum); ---> $("input").next(".result").text(sum);

– son pham
Nov 23 '18 at 8:22





in tag span you must use function text set value for it. $("input").next(".result").val(sum); ---> $("input").next(".result").text(sum);

– son pham
Nov 23 '18 at 8:22












2 Answers
2






active

oldest

votes


















1














From the OP it is understand that .result is a span so $("input").next(".result").val(sum); won't work because span does not have a function called .val() use .text() instead. And also change



 var rank = $("input").next(".rank").val();


to



var rank = $(this).next(".rank").val();


DEMO HERE



EDIT



Based on OP's comment. try this



$(document).on("change", ".parent", function() {
var sum = 0;
$(".parent").each(function() {
var rank = $(this).next(".rank").val();
sum = $(this).val() * rank;
$(this).parent().find(".result").text(sum);
});

});


UPDATED DEMO






share|improve this answer


























  • Thank you for your answer! It still not working correctly. When I set some value into one input, it returns 0 into all result spans.

    – Johann G
    Nov 23 '18 at 8:52











  • what is your expecting output?

    – Kiranramchandran
    Nov 23 '18 at 8:53











  • Example: When I set 10 to <input name="1" class="parent"> it will be multiplied by value <input name="rank" value="8">, it means it returns 80 to <span class="result"> in <li id="1">

    – Johann G
    Nov 23 '18 at 9:25











  • check updated answer.

    – Kiranramchandran
    Nov 23 '18 at 9:33











  • thank you so much!

    – Johann G
    Nov 23 '18 at 9:39



















0














Have you try



var rank = $(this).siblings(".rank").val();

$(this).siblings(".result").val(sum);





share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53442812%2fmultiply-input-values-into-span%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    From the OP it is understand that .result is a span so $("input").next(".result").val(sum); won't work because span does not have a function called .val() use .text() instead. And also change



     var rank = $("input").next(".rank").val();


    to



    var rank = $(this).next(".rank").val();


    DEMO HERE



    EDIT



    Based on OP's comment. try this



    $(document).on("change", ".parent", function() {
    var sum = 0;
    $(".parent").each(function() {
    var rank = $(this).next(".rank").val();
    sum = $(this).val() * rank;
    $(this).parent().find(".result").text(sum);
    });

    });


    UPDATED DEMO






    share|improve this answer


























    • Thank you for your answer! It still not working correctly. When I set some value into one input, it returns 0 into all result spans.

      – Johann G
      Nov 23 '18 at 8:52











    • what is your expecting output?

      – Kiranramchandran
      Nov 23 '18 at 8:53











    • Example: When I set 10 to <input name="1" class="parent"> it will be multiplied by value <input name="rank" value="8">, it means it returns 80 to <span class="result"> in <li id="1">

      – Johann G
      Nov 23 '18 at 9:25











    • check updated answer.

      – Kiranramchandran
      Nov 23 '18 at 9:33











    • thank you so much!

      – Johann G
      Nov 23 '18 at 9:39
















    1














    From the OP it is understand that .result is a span so $("input").next(".result").val(sum); won't work because span does not have a function called .val() use .text() instead. And also change



     var rank = $("input").next(".rank").val();


    to



    var rank = $(this).next(".rank").val();


    DEMO HERE



    EDIT



    Based on OP's comment. try this



    $(document).on("change", ".parent", function() {
    var sum = 0;
    $(".parent").each(function() {
    var rank = $(this).next(".rank").val();
    sum = $(this).val() * rank;
    $(this).parent().find(".result").text(sum);
    });

    });


    UPDATED DEMO






    share|improve this answer


























    • Thank you for your answer! It still not working correctly. When I set some value into one input, it returns 0 into all result spans.

      – Johann G
      Nov 23 '18 at 8:52











    • what is your expecting output?

      – Kiranramchandran
      Nov 23 '18 at 8:53











    • Example: When I set 10 to <input name="1" class="parent"> it will be multiplied by value <input name="rank" value="8">, it means it returns 80 to <span class="result"> in <li id="1">

      – Johann G
      Nov 23 '18 at 9:25











    • check updated answer.

      – Kiranramchandran
      Nov 23 '18 at 9:33











    • thank you so much!

      – Johann G
      Nov 23 '18 at 9:39














    1












    1








    1







    From the OP it is understand that .result is a span so $("input").next(".result").val(sum); won't work because span does not have a function called .val() use .text() instead. And also change



     var rank = $("input").next(".rank").val();


    to



    var rank = $(this).next(".rank").val();


    DEMO HERE



    EDIT



    Based on OP's comment. try this



    $(document).on("change", ".parent", function() {
    var sum = 0;
    $(".parent").each(function() {
    var rank = $(this).next(".rank").val();
    sum = $(this).val() * rank;
    $(this).parent().find(".result").text(sum);
    });

    });


    UPDATED DEMO






    share|improve this answer















    From the OP it is understand that .result is a span so $("input").next(".result").val(sum); won't work because span does not have a function called .val() use .text() instead. And also change



     var rank = $("input").next(".rank").val();


    to



    var rank = $(this).next(".rank").val();


    DEMO HERE



    EDIT



    Based on OP's comment. try this



    $(document).on("change", ".parent", function() {
    var sum = 0;
    $(".parent").each(function() {
    var rank = $(this).next(".rank").val();
    sum = $(this).val() * rank;
    $(this).parent().find(".result").text(sum);
    });

    });


    UPDATED DEMO







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 23 '18 at 9:33

























    answered Nov 23 '18 at 8:24









    KiranramchandranKiranramchandran

    1,9881128




    1,9881128













    • Thank you for your answer! It still not working correctly. When I set some value into one input, it returns 0 into all result spans.

      – Johann G
      Nov 23 '18 at 8:52











    • what is your expecting output?

      – Kiranramchandran
      Nov 23 '18 at 8:53











    • Example: When I set 10 to <input name="1" class="parent"> it will be multiplied by value <input name="rank" value="8">, it means it returns 80 to <span class="result"> in <li id="1">

      – Johann G
      Nov 23 '18 at 9:25











    • check updated answer.

      – Kiranramchandran
      Nov 23 '18 at 9:33











    • thank you so much!

      – Johann G
      Nov 23 '18 at 9:39



















    • Thank you for your answer! It still not working correctly. When I set some value into one input, it returns 0 into all result spans.

      – Johann G
      Nov 23 '18 at 8:52











    • what is your expecting output?

      – Kiranramchandran
      Nov 23 '18 at 8:53











    • Example: When I set 10 to <input name="1" class="parent"> it will be multiplied by value <input name="rank" value="8">, it means it returns 80 to <span class="result"> in <li id="1">

      – Johann G
      Nov 23 '18 at 9:25











    • check updated answer.

      – Kiranramchandran
      Nov 23 '18 at 9:33











    • thank you so much!

      – Johann G
      Nov 23 '18 at 9:39

















    Thank you for your answer! It still not working correctly. When I set some value into one input, it returns 0 into all result spans.

    – Johann G
    Nov 23 '18 at 8:52





    Thank you for your answer! It still not working correctly. When I set some value into one input, it returns 0 into all result spans.

    – Johann G
    Nov 23 '18 at 8:52













    what is your expecting output?

    – Kiranramchandran
    Nov 23 '18 at 8:53





    what is your expecting output?

    – Kiranramchandran
    Nov 23 '18 at 8:53













    Example: When I set 10 to <input name="1" class="parent"> it will be multiplied by value <input name="rank" value="8">, it means it returns 80 to <span class="result"> in <li id="1">

    – Johann G
    Nov 23 '18 at 9:25





    Example: When I set 10 to <input name="1" class="parent"> it will be multiplied by value <input name="rank" value="8">, it means it returns 80 to <span class="result"> in <li id="1">

    – Johann G
    Nov 23 '18 at 9:25













    check updated answer.

    – Kiranramchandran
    Nov 23 '18 at 9:33





    check updated answer.

    – Kiranramchandran
    Nov 23 '18 at 9:33













    thank you so much!

    – Johann G
    Nov 23 '18 at 9:39





    thank you so much!

    – Johann G
    Nov 23 '18 at 9:39













    0














    Have you try



    var rank = $(this).siblings(".rank").val();

    $(this).siblings(".result").val(sum);





    share|improve this answer






























      0














      Have you try



      var rank = $(this).siblings(".rank").val();

      $(this).siblings(".result").val(sum);





      share|improve this answer




























        0












        0








        0







        Have you try



        var rank = $(this).siblings(".rank").val();

        $(this).siblings(".result").val(sum);





        share|improve this answer















        Have you try



        var rank = $(this).siblings(".rank").val();

        $(this).siblings(".result").val(sum);






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 23 '18 at 9:48









        Mohammad

        15.9k123766




        15.9k123766










        answered Nov 23 '18 at 9:25









        Mishen DaishikaMishen Daishika

        765




        765






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53442812%2fmultiply-input-values-into-span%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

            Alcedinidae

            Origin of the phrase “under your belt”?