'Change value of a textbox, compared to another text box value with a ratio
I am working on ASP.NET C#.
With Javascript, I need to change the value of a textbox, when another text box value changes with the ratio of 3:2.
For example, if the value in a text is 150, the other textbox value should be 100.
Solution 1:[1]
Try this,
Javascript:
<script type="text/javascript">
function fill() {
document.getElementsByName('txt2')[0].value) = (document.getElementsByName('txt1')[0].value) * (2 / 3);
}
</script>
Code in body tag:
<form id="form1" runat="server">
<div>
<%--The text boxes must have numeric values--%>
<asp:TextBox ID="txt1" runat="server" onchange="javascript:fill();"></asp:TextBox>
<asp:TextBox ID="txt2" runat="server"></asp:TextBox>
</div>
Hope this helps.
Solution 2:[2]
use something like this
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function change() {
var t1Val = (document.getElementById("t1")).value;
(document.getElementById("t2")).value = ((t1Val * 2) / 3);
};
</script>
</head>
<body>
<input id="t1" type="text" onchange="change()"/>
<input id="t2" type="text"/>
</body>
</html>
If is there any validation you need you can write inside change() function.
Solution 3:[3]
You may use onchange
or onkeyup
events on the text box
Add the following java-script function to your html
function ChangeValue() {
if (document.getElementById('txtval1').value) {
document.getElementById('txtval2').value=document.getElementById('txtval1').value*2/3;
}
}
then add two text-boxes with ids txtval1,txtval2 to your html, add onkeyup="ChangeValue();
to the first one
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | |
Solution 3 | GDP |