Update input value dynamically & update another element after dynamic input change
up vote
0
down vote
favorite
Below is the sample HTML and JS that I created for the problem that I am having. I have a form that has three inputs. The value from the first two inputs will dictate the value of the third. I also want the values of all three inputs to display in <p>
tags outside of the form. I have been able to update the first two <p>
tags outside the form correctly, and I have been able to update the third input value correctly. However, I cannot get the third <p>
tag to update when the third input value has dynamically changed.
Here is a Codepen for the problem.
HTML
<form>
<input id="one" type="number" class="sumVari" name="a">
<input id="two" type="number" class="sumVari" name="b">
<input id="three" type="number" name="sum" placeholder="sum of a & b">
</form>
<div id="displayValues">
<p>The value of a is: <span id="a"></span> </p>
<p>The value of a is: <span id="b"></span></p>
<p>The value of the sum is: <span id="sum"></span></p>
</div>
JS
let one = $('#one');
let two = $('#two');
let three = $('#three');
$('form').on('change', 'input', function() {
let target = $(this);
let attrName = target.attr('name');
$('#'+attrName).text(target.val());
});
function sum(a,b) {
return a+b;
}
$('.sumVari').each(function(){
$(this).change(function(){
if(one.val() != '' && two.val() != '') {
three.val(sum(one.val(), two.val()));
}
});
});
jquery forms events input dynamically-generated
add a comment |
up vote
0
down vote
favorite
Below is the sample HTML and JS that I created for the problem that I am having. I have a form that has three inputs. The value from the first two inputs will dictate the value of the third. I also want the values of all three inputs to display in <p>
tags outside of the form. I have been able to update the first two <p>
tags outside the form correctly, and I have been able to update the third input value correctly. However, I cannot get the third <p>
tag to update when the third input value has dynamically changed.
Here is a Codepen for the problem.
HTML
<form>
<input id="one" type="number" class="sumVari" name="a">
<input id="two" type="number" class="sumVari" name="b">
<input id="three" type="number" name="sum" placeholder="sum of a & b">
</form>
<div id="displayValues">
<p>The value of a is: <span id="a"></span> </p>
<p>The value of a is: <span id="b"></span></p>
<p>The value of the sum is: <span id="sum"></span></p>
</div>
JS
let one = $('#one');
let two = $('#two');
let three = $('#three');
$('form').on('change', 'input', function() {
let target = $(this);
let attrName = target.attr('name');
$('#'+attrName).text(target.val());
});
function sum(a,b) {
return a+b;
}
$('.sumVari').each(function(){
$(this).change(function(){
if(one.val() != '' && two.val() != '') {
three.val(sum(one.val(), two.val()));
}
});
});
jquery forms events input dynamically-generated
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Below is the sample HTML and JS that I created for the problem that I am having. I have a form that has three inputs. The value from the first two inputs will dictate the value of the third. I also want the values of all three inputs to display in <p>
tags outside of the form. I have been able to update the first two <p>
tags outside the form correctly, and I have been able to update the third input value correctly. However, I cannot get the third <p>
tag to update when the third input value has dynamically changed.
Here is a Codepen for the problem.
HTML
<form>
<input id="one" type="number" class="sumVari" name="a">
<input id="two" type="number" class="sumVari" name="b">
<input id="three" type="number" name="sum" placeholder="sum of a & b">
</form>
<div id="displayValues">
<p>The value of a is: <span id="a"></span> </p>
<p>The value of a is: <span id="b"></span></p>
<p>The value of the sum is: <span id="sum"></span></p>
</div>
JS
let one = $('#one');
let two = $('#two');
let three = $('#three');
$('form').on('change', 'input', function() {
let target = $(this);
let attrName = target.attr('name');
$('#'+attrName).text(target.val());
});
function sum(a,b) {
return a+b;
}
$('.sumVari').each(function(){
$(this).change(function(){
if(one.val() != '' && two.val() != '') {
three.val(sum(one.val(), two.val()));
}
});
});
jquery forms events input dynamically-generated
Below is the sample HTML and JS that I created for the problem that I am having. I have a form that has three inputs. The value from the first two inputs will dictate the value of the third. I also want the values of all three inputs to display in <p>
tags outside of the form. I have been able to update the first two <p>
tags outside the form correctly, and I have been able to update the third input value correctly. However, I cannot get the third <p>
tag to update when the third input value has dynamically changed.
Here is a Codepen for the problem.
HTML
<form>
<input id="one" type="number" class="sumVari" name="a">
<input id="two" type="number" class="sumVari" name="b">
<input id="three" type="number" name="sum" placeholder="sum of a & b">
</form>
<div id="displayValues">
<p>The value of a is: <span id="a"></span> </p>
<p>The value of a is: <span id="b"></span></p>
<p>The value of the sum is: <span id="sum"></span></p>
</div>
JS
let one = $('#one');
let two = $('#two');
let three = $('#three');
$('form').on('change', 'input', function() {
let target = $(this);
let attrName = target.attr('name');
$('#'+attrName).text(target.val());
});
function sum(a,b) {
return a+b;
}
$('.sumVari').each(function(){
$(this).change(function(){
if(one.val() != '' && two.val() != '') {
three.val(sum(one.val(), two.val()));
}
});
});
jquery forms events input dynamically-generated
jquery forms events input dynamically-generated
asked Nov 12 at 0:24
prestondoris
395
395
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
- Call the change event manually when putting value on the sum
let one = $('#one');
let two = $('#two');
let three = $('#three');
$('form').on('change', 'input', function() {
let target = $(this);
let attrName = target.attr('name');
$('#' + attrName).text(target.val());
});
function sum(a, b) {
return a + b;
}
$('.sumVari').each(function() {
$(this).change(function() {
if (one.val() != '' && two.val() != '') {
three.val(sum(one.val(), two.val())).change();//call the change event manually here on the sum input so that the change function will run on the sum input
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
<input id="one" type="number" class="sumVari" name="a">
<input id="two" type="number" class="sumVari" name="b">
<input id="three" type="number" name="sum" placeholder="sum of a & b">
</form>
<div id="displayValues">
<p>The value of a is: <span id="a"></span> </p>
<p>The value of a is: <span id="b"></span></p>
<p>The value of the sum is: <span id="sum"></span></p>
</div>
@prestondoris glad it works for you happy coding mate :)
– guradio
Nov 13 at 0:25
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
- Call the change event manually when putting value on the sum
let one = $('#one');
let two = $('#two');
let three = $('#three');
$('form').on('change', 'input', function() {
let target = $(this);
let attrName = target.attr('name');
$('#' + attrName).text(target.val());
});
function sum(a, b) {
return a + b;
}
$('.sumVari').each(function() {
$(this).change(function() {
if (one.val() != '' && two.val() != '') {
three.val(sum(one.val(), two.val())).change();//call the change event manually here on the sum input so that the change function will run on the sum input
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
<input id="one" type="number" class="sumVari" name="a">
<input id="two" type="number" class="sumVari" name="b">
<input id="three" type="number" name="sum" placeholder="sum of a & b">
</form>
<div id="displayValues">
<p>The value of a is: <span id="a"></span> </p>
<p>The value of a is: <span id="b"></span></p>
<p>The value of the sum is: <span id="sum"></span></p>
</div>
@prestondoris glad it works for you happy coding mate :)
– guradio
Nov 13 at 0:25
add a comment |
up vote
1
down vote
accepted
- Call the change event manually when putting value on the sum
let one = $('#one');
let two = $('#two');
let three = $('#three');
$('form').on('change', 'input', function() {
let target = $(this);
let attrName = target.attr('name');
$('#' + attrName).text(target.val());
});
function sum(a, b) {
return a + b;
}
$('.sumVari').each(function() {
$(this).change(function() {
if (one.val() != '' && two.val() != '') {
three.val(sum(one.val(), two.val())).change();//call the change event manually here on the sum input so that the change function will run on the sum input
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
<input id="one" type="number" class="sumVari" name="a">
<input id="two" type="number" class="sumVari" name="b">
<input id="three" type="number" name="sum" placeholder="sum of a & b">
</form>
<div id="displayValues">
<p>The value of a is: <span id="a"></span> </p>
<p>The value of a is: <span id="b"></span></p>
<p>The value of the sum is: <span id="sum"></span></p>
</div>
@prestondoris glad it works for you happy coding mate :)
– guradio
Nov 13 at 0:25
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
- Call the change event manually when putting value on the sum
let one = $('#one');
let two = $('#two');
let three = $('#three');
$('form').on('change', 'input', function() {
let target = $(this);
let attrName = target.attr('name');
$('#' + attrName).text(target.val());
});
function sum(a, b) {
return a + b;
}
$('.sumVari').each(function() {
$(this).change(function() {
if (one.val() != '' && two.val() != '') {
three.val(sum(one.val(), two.val())).change();//call the change event manually here on the sum input so that the change function will run on the sum input
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
<input id="one" type="number" class="sumVari" name="a">
<input id="two" type="number" class="sumVari" name="b">
<input id="three" type="number" name="sum" placeholder="sum of a & b">
</form>
<div id="displayValues">
<p>The value of a is: <span id="a"></span> </p>
<p>The value of a is: <span id="b"></span></p>
<p>The value of the sum is: <span id="sum"></span></p>
</div>
- Call the change event manually when putting value on the sum
let one = $('#one');
let two = $('#two');
let three = $('#three');
$('form').on('change', 'input', function() {
let target = $(this);
let attrName = target.attr('name');
$('#' + attrName).text(target.val());
});
function sum(a, b) {
return a + b;
}
$('.sumVari').each(function() {
$(this).change(function() {
if (one.val() != '' && two.val() != '') {
three.val(sum(one.val(), two.val())).change();//call the change event manually here on the sum input so that the change function will run on the sum input
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
<input id="one" type="number" class="sumVari" name="a">
<input id="two" type="number" class="sumVari" name="b">
<input id="three" type="number" name="sum" placeholder="sum of a & b">
</form>
<div id="displayValues">
<p>The value of a is: <span id="a"></span> </p>
<p>The value of a is: <span id="b"></span></p>
<p>The value of the sum is: <span id="sum"></span></p>
</div>
let one = $('#one');
let two = $('#two');
let three = $('#three');
$('form').on('change', 'input', function() {
let target = $(this);
let attrName = target.attr('name');
$('#' + attrName).text(target.val());
});
function sum(a, b) {
return a + b;
}
$('.sumVari').each(function() {
$(this).change(function() {
if (one.val() != '' && two.val() != '') {
three.val(sum(one.val(), two.val())).change();//call the change event manually here on the sum input so that the change function will run on the sum input
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
<input id="one" type="number" class="sumVari" name="a">
<input id="two" type="number" class="sumVari" name="b">
<input id="three" type="number" name="sum" placeholder="sum of a & b">
</form>
<div id="displayValues">
<p>The value of a is: <span id="a"></span> </p>
<p>The value of a is: <span id="b"></span></p>
<p>The value of the sum is: <span id="sum"></span></p>
</div>
let one = $('#one');
let two = $('#two');
let three = $('#three');
$('form').on('change', 'input', function() {
let target = $(this);
let attrName = target.attr('name');
$('#' + attrName).text(target.val());
});
function sum(a, b) {
return a + b;
}
$('.sumVari').each(function() {
$(this).change(function() {
if (one.val() != '' && two.val() != '') {
three.val(sum(one.val(), two.val())).change();//call the change event manually here on the sum input so that the change function will run on the sum input
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
<input id="one" type="number" class="sumVari" name="a">
<input id="two" type="number" class="sumVari" name="b">
<input id="three" type="number" name="sum" placeholder="sum of a & b">
</form>
<div id="displayValues">
<p>The value of a is: <span id="a"></span> </p>
<p>The value of a is: <span id="b"></span></p>
<p>The value of the sum is: <span id="sum"></span></p>
</div>
answered Nov 12 at 0:28
guradio
13.7k31836
13.7k31836
@prestondoris glad it works for you happy coding mate :)
– guradio
Nov 13 at 0:25
add a comment |
@prestondoris glad it works for you happy coding mate :)
– guradio
Nov 13 at 0:25
@prestondoris glad it works for you happy coding mate :)
– guradio
Nov 13 at 0:25
@prestondoris glad it works for you happy coding mate :)
– guradio
Nov 13 at 0:25
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53254591%2fupdate-input-value-dynamically-update-another-element-after-dynamic-input-chan%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