Multiply input values into span
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
add a comment |
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
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
add a comment |
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
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
jquery next closest multiplying
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
add a comment |
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
add a comment |
2 Answers
2
active
oldest
votes
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
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
|
show 1 more comment
Have you try
var rank = $(this).siblings(".rank").val();
$(this).siblings(".result").val(sum);
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
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
|
show 1 more comment
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
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
|
show 1 more comment
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
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
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
|
show 1 more comment
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
|
show 1 more comment
Have you try
var rank = $(this).siblings(".rank").val();
$(this).siblings(".result").val(sum);
add a comment |
Have you try
var rank = $(this).siblings(".rank").val();
$(this).siblings(".result").val(sum);
add a comment |
Have you try
var rank = $(this).siblings(".rank").val();
$(this).siblings(".result").val(sum);
Have you try
var rank = $(this).siblings(".rank").val();
$(this).siblings(".result").val(sum);
edited Nov 23 '18 at 9:48
Mohammad
15.9k123766
15.9k123766
answered Nov 23 '18 at 9:25
Mishen DaishikaMishen Daishika
765
765
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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