This question already has answers here:
Substracting values on keyup using jquery
(3 answers)
Closed 9 years ago.
I have two textbox as follow:
<input id="amount" name="amount" type="text" value="0" />
<input id="discount" name="discount" type="text" value="0" />
I want substract discount from amount, and add result to another textbox. How can I do it?
$("#result").val(parseInt($("#amount").val(), 10) - parseInt($("#discount").val(), 10));
That should do it. jsFiddle here.
You can do this using JavaScript/Jquery
Suppose your third textbox is
Then your Jquery code will be
var vResult = parseInt( $('#amount').val() ) - parseInt( $('#discount').val() );
$('#result').val( vResult );
Related
This question already has answers here:
How do I get the value of text input field using JavaScript?
(16 answers)
Closed 1 year ago.
hi i have javascript issue that i wrote a input when i change it text to sadra it doesn't alert "your text contains "sadra" word " how can i resolve it ? thanks for helping ...
here is the code:
var realText;
function text(realText) {
var realText = document.getElementById('input1').innerHTML;
if (realText.includes('sadra')) {
alert("your text contains \"sadra\" word ");
}
}
<label>Input :</label>
<input type="text" name="test" id="input1" onchange="text(realText)" />
If you are trying to access the value inside the element with id input1, then the selector should point to value of the node.
document.getElementById('input1').value
Also there is no need to pass the realText parameter to the onchange event. This is not going to have an impact on the functionality.
innerHTML gives the html code inside a specified id
innerHTML Example
function text() {
var realText = document.getElementById('container').innerHTML;
console.log('Inner HTML', realText);
}
<div id="container">
<label>Input :</label>
<input type="text" name="test" id="input1" onchange="text()" />
</div>
Your Working fiddle
function text() {
var realText = document.getElementById('input1').value;
if (realText.includes('sadra')) {
alert("your text contains \"sadra\" word ");
}
}
<label>Input :</label>
<input type="text" name="test" id="input1" onchange="text()" />
This question already has answers here:
How to force JS to do math instead of putting two strings together [duplicate]
(11 answers)
How to force addition instead of concatenation in javascript [duplicate]
(3 answers)
Closed 3 years ago.
Here are my code and its showing 00.000.000.00. I have changed this code another way then it's showing only the first digit of the result.
$(document).on('change', '.prc', function() {
var tSum = 0;
$('.prc').each(function() {
tSum += parseFloat($(this).val());
});
$('.totalprc').val(tSum);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input class="prc" value="0.00" />
<input class="prc" value="0.00" />
<input class="prc" value="0.00" />
<input class="totalprc" />
This question already has answers here:
How to get the selected radio button’s value?
(19 answers)
Closed 8 years ago.
undefined output!! am i missing sth ? any idea ?
here is the radio button inputs :
<tr>
<td>Yes <input type="radio" name="email" id="email" value="yes"/></td>
<td></td>
<td>No <input type="radio" name="email" id="email" value="no"checked="checked"/></td>
</tr>
<input type="button" value="submit" onclick="doIt()" style="width: 150px; height:30px" />
<script type="text/javascript" language="javascript">
function doIt() {
var emailopt = document.getElementById('email').checked.value;
alert (emailopt);
}
The way you have tried is completely wrong. Instead use the below approach
1) Iterate through all the radio buttons with name property
2) Now using checked attribute, place an if statement and then extract the value
function doIt() {
var emailopt = document.getElementsByName('email');
for (i = 0; i < emailopt.length; i++) {
if (emailopt[i].checked) {
alert(emailopt[i].value)
}
}
}
Fiddle
Try using this:
function doIt() {
var emailopt = document.getElementById('email').checked;
alert (emailopt);
}
It will return true and false.
Lets me know if you have any questions.I hope it will help you.
This question already has answers here:
Formatting a number with exactly two decimals in JavaScript
(32 answers)
How to round float numbers in javascript?
(19 answers)
Closed 9 years ago.
I am working on a calculator and would like it to round to about 5 decimal places. Here is my javascript code to get an inout and produce the output:
<div class="input">
Price paid per share: <input id="value1" type="text" /><br>
Number of shares bought: <input id="value2" type="text" /><br>
Commission/ fee's paid: <input id="value3" type="text" /><br>
<center><input type="submit" class="submit" onclick="output();"></center>
</div>
<center><p class="result" id="result"> </p></center>
<script type="text/javascript" language="javascript" charset="utf-8">
function output(){
var value1 = document.getElementById('value1').value;
var value2 = document.getElementById('value2').value;
var value3 = document.getElementById('value3').value;
document.getElementById('result').innerHTML = ((parseFloat(value1) * parseFloat(value2)) + (parseFloat(value3))) / (parseFloat(value2));
}
Anyone know how I could round my output/ results to about 5 decimal places? Thanks.
var num=4.5960797;
var n=num.toFixed(5);
Try using toFixed:
var result = ((parseFloat(value1) * parseFloat(value2)) + (parseFloat(value3))) / (parseFloat(value2));
document.getElementById('result').innerHTML = result.toFixed(5);
Use Math.round(num * 100000) / 100000
var result=Math.round(yourValue*100000)/100000
This question already has answers here:
Radio Button and an Input field
(3 answers)
Closed 8 years ago.
im trying to activate/disable a textinput by activating/disabling a radio button.
html:
<input name="test" type="radio" value="one" onclick="activate();"/>
<input class="" name="info" type="text" size="5" maxlength="5" disabled>
<input name="test" type="radio" value="two""/>
javascript:
function activate(){
document.forms[0].info.disabled = !document.forms[0].test[0].checked;
}
when u activate the 1st radio button, the input text should be activated. when u activate 2nd button, it should be disabled.
but this code doesnt work. does anyone know better?
You need to run the activate function again when the other radio button is clicked.
Try this:
window.onload=function() {
var rad = document.getElementsByName("test");
for (var i=0;i<rad.length;i++) {
rad[i].onclick=function() {
this.form.info.disabled=this.value!="one";
}
}