I'm currently using the following solution to generate a number based on user input into two form fields http://jsfiddle.net/A3qat/5/
$("#download, #contention").keyup( function(){
var n1 = $("#download").val();
var n2 = $("#contention").val();
var result = n1/n2;
$("#label").text(result);
});
So, n1/n2 = n3
How can I modify this to show the output (n3) in a separate input form field underneath rather than plain text
Fiddle: http://jsfiddle.net/A3qat/49/
You should use .val()
$("#field1, #field2").keyup( function(){
var n1 = $("#field1").val();
var n2 = $("#field2").val();
var result = n2/n1;
$("#label").val(result);
});
Replace your label with input and replace $("#label").text(result); with $("#label").val(result);
<input type="number" id="field1">
<input type="number" id="field2">
<input id="result"></input>
<script>
$("#download, #contention").keyup( function(){
var n1 = $("#download").val();
var n2 = $("#contention").val();
var result = n1/n2;
$("#result").val(result);
});
</script>
DEMO:
http://jsfiddle.net/A3qat/51/
If you textbox, then you can get the value of the textbox or you can set the value of the textbox using .val()
On your
HTML
<input type="number" id="field1">
<input type="number" id="field2">
<span><input type="text" id="label" ></span>
JS
$("#field1, #field2").keyup( function(){
var n1 = $("#field1").val();
var n2 = $("#field2").val();
var result = n2/n1;
$("#label").val(result);
});
Related
https://codepen.io/kev_daddy/pen/MMWEMG
I am building a form that is meant to update the difference between two values in real time (ie without refreshing the page). It is comprised of multiple fields, but ultimately I'll be getting the sum of two values, and displaying this using HTML.
The entire thing appears to work as intended until I get to the function that is meant to display the sum in html.
The intention is that the result (a hidden field) is shown as plain text in output. It doesn't trigger on the onset, however if i punch in an extra character using my keyboard, the event is finally heard and the text shows. up.
I am sure that I am missing something, but how do I ensure that the sum is outputted?
function calculate() {
var x = document.getElementById('fee_selector_holder').value || 0;
var y = document.getElementById('content').value || 0;
var result = document.getElementById('result');
var myResult = parseInt(x) + parseInt(y);
result.value = myResult; }
var input = document.getElementById("result");
var output = document.getElementById("output"); input.addEventListener("input", function() {
output.innerText = this.value;
});
<input type="text" name="hostelfees" id="content" oninput="calculate()">
<input type="text" name="fee_id" id="fee_selector_holder" oninput="calculate()">
<input type="text" id="result" name="totalfee">
<hr>
<p>You can earn <span id="output"></span> more!
There is no input event on span. You can create a separate function and pass the value of the calculation to this function whose responsibility will be to update the span text content
function calculate() {
var x = document.getElementById('fee_selector_holder').value || 0;
var y = document.getElementById('content').value || 0;
var result = document.getElementById('result');
var myResult = parseInt(x) + parseInt(y);
result.value = myResult;
updateText(myResult)
}
function updateText(val) {
document.getElementById("output").innerText = val;
}
<input type="text" name="hostelfees" id="content" oninput="calculate()">
<input type="text" name="fee_id" id="fee_selector_holder" oninput="calculate()">
<input type="text" id="result" name="totalfee">
<hr>
<p>You can earn <span id="output"></span> more!
I want to copy the date text to textfield. when i click the button to copy, the result in textfield is undefined. Can someone help me about this problem?
function ShowHideDiv() {
var date1 = document.getElementById("datetime");
var text1 = document.getElementById("textfield1");
text1.value = date1.value;
}
var dt = new Date();
document.getElementById("datetime").innerHTML = dt.toISOString().substr(0, 10);
<p name="datetime" id="datetime" style="position:absolute;margin-top:80px;font-weight:bold"><span id="datetime" name="datetime" id="date_now"></span></p>
<input type="text" id="textfield1" name="textfield1">
<input type="submit" onclick="ShowHideDiv()" value="copy">
You are setting the date as innerHTML of the paragraph tag. So, instead of date1.value use date1.innerHTML. Check the working snippet:
function ShowHideDiv() {
var date1 = document.getElementById("datetime");
var text1 = document.getElementById("textfield1");
text1.value = date1.innerHTML;
}
var dt = new Date();
document.getElementById("datetime").innerHTML = dt.toISOString().substr(0, 10);
<p name="datetime" id="datetime" style="position:absolute;margin-top:80px;font-weight:bold"><span id="datetime" name="datetime" id="date_now"></span></p>
<input type="text" id="textfield1" name="textfield1">
<input type="submit" onclick="ShowHideDiv()" value="copy">
You need to use innerHTML which will provide the inner content of selected element by id.
function ShowHideDiv() {
var date1 = document.getElementById("datetime");
var text1 = document.getElementById("textfield1");
text1.value = date1.innerHTML;
}
var dt = new Date();document.getElementById("datetime").innerHTML = dt.toISOString().substr(0, 10);
The innerHTML property sets or returns the HTML content (inner HTML) of an element.
Also you need to use button instead of using input with type submit, since you are not submitting the data. This approach will be more fast and better.
<button type="button" onclick="ShowHideDiv()" >Copy</button>
Hi I'm new in the community.
I am trying to create a simple page where in there are 3 textbox. 1st text box is where the number will be entered. For 2nd and 3rd textbox is where the result will be shows on a different format as soon as the numbers are entered from the 1st textbox. 2nd text box should show the number with a comma which I was able to do. Example: As soon as I enter a number on the first text box 22 55 01 02 the 2nd text box will show 22,55,01,02 however on the 3rd textbox it should show the same number from 2nd textbox but on Ascending order which I weren't able to do so. Tried searching for a solution already but to now avail. Maybe I am just missing something. Any help will be very much appreciated.
function boxx1KeyPress() {
var boxx1 = document.getElementById("boxx1");
var s = boxx1.value.replace(/[ ,]+/g, ",");
var x = s;
var lblValue = document.getElementById("boxx2");
lblValue.value = "" + s;
// code for textbox 3 that didn't work
//function sortAscending(a, b)
//{return a - b;
// }
//var points = boxx3.value;
//points.sort(sortAscending);
//document.getElementById("boxx3").innerHTML = points;
}
function ClearField() {
document.getElementById("boxx1").value = "";
document.getElementById("boxx2").value = "";
document.getElementById("boxx3").value = "";
}
<body>
<B><br><center>PASTE HERE</br>
<input id="boxx1" type="text" onKeyPress="boxx1KeyPress()"
onKeyUp="boxx1KeyPress()">
<br>
<input type="button" Value="Clear Field" onClick="ClearField()">
<br>
<br>11x5 GAMES</BR>
<span id="lblValue"></span>
<input id="boxx2" type="text">
<br>
<br>Keno Games</br>
<input id="boxx3" type="text">
<br>
<p id="Keno"></p>
<input type="button" Value="Ascend" onClick="points.sort(sortAscending)">
</body>
It's actually incredibly simple to sort numbers in JavaScript. All you need to do is:
Split the initial string into an array with .split(" ") (splitting on a space).
Sort the numbers with .sort().
Join the numbers back to a string with .join().
Keep in mind that as the output box is an <input>, you'll need to use .value instead of .innerHTML:
function boxx1KeyPress() {
var boxx1 = document.getElementById("boxx1");
var s = boxx1.value.replace(/[ ,]+/g, ",");
var x = s;
var lblValue = document.getElementById("boxx2");
lblValue.value = "" + s;
// Fixed code for sorting the numbers
var points = boxx1.value.split(" ");
document.getElementById("boxx3").value = points.sort().join();
}
function ClearField() {
document.getElementById("boxx1").value = "";
document.getElementById("boxx2").value = "";
document.getElementById("boxx3").value = "";
}
<body>
<br>
<center>
<b>PASTE HERE</b>
<input id="boxx1" type="text" onKeyPress="boxx1KeyPress()" onKeyUp="boxx1KeyPress()">
<br>
<br>
<input type="button" Value="Clear Field" onClick="ClearField()">
<br>
<br>
11x5 GAMES
<span id="lblValue"></span>
<input id="boxx2" type="text">
<br>
<br>
Keno Games
<input id="boxx3" type="text">
<br>
<p id="Keno"></p>
<input type="button" Value="Ascend">
</center>
</body>
Also note that you had some slightly invalid HTML in your above snippet (primarily that <br> is a void element, so the tag self-closes and thus </br> is not valid). I've cleaned up the HTML in my snippet above.
Hope this helps! :)
Your main issue is that you are trying to sort something that is still a string... you have to make your string into an array first.
function boxx1KeyPress() {
var boxx1 = document.getElementById("boxx1");
var s = boxx1.value.replace(/[ ,]+/g, ",");
var x = s;
var lblValue = document.getElementById("boxx2");
lblValue.value = "" + s;
// get an array from our string s
var arr = s.split(',');
arr.sort(); // note that for strings or ints, the default sort is ascending
document.getElementById("boxx3").innerHTML = arr.join(',');
}
I used the String.split method to get an array, separated at the commas, and the Array.join method to turn it back into a string after it was sorted.
Convert comma separated string into Array. Use array sort function and you done.
function boxx1KeyPress() {
var boxx1 = document.getElementById("boxx1");
var s = boxx1.value.replace(/[ ,]+/g, ",");
var lblValue = document.getElementById("boxx2");
lblValue.value = "" + s;
}
function sortDisending() {
var numberArray = document.getElementById("boxx2").value.split(",");
numberArray.sort(function(a, b){return b-a});
document.getElementById("boxx3").value = numberArray;
}
function sortAsending() {
var numberArray = document.getElementById("boxx2").value.split(",");
numberArray.sort(function(a, b){return a-b});
document.getElementById("boxx3").value = numberArray;
}
function ClearField() {
document.getElementById("boxx1").value = "";
document.getElementById("boxx2").value = "";
document.getElementById("boxx3").value = "";
}
<B><br><center>PASTE HERE
<input id="boxx1" type="text" onKeyPress="boxx1KeyPress()"
onKeyUp="boxx1KeyPress()">
<br>
<br>
<br>11x5 GAMES
<span id="lblValue"></span>
<input id="boxx2" type="text">
<br>
<br>Keno Games
<input id="boxx3" type="text">
<br>
<p id="Keno"></p>
<input type="button" Value="Ascend" onClick="sortAsending()">
<input type="button" Value="Descend" onClick="sortDisending()">
<input type="button" Value="Clear Field" onClick="ClearField()">
Fiddle Demo
here's the JSfiddle for it, can't seem to get the read-only input to update with the val of the other input (it's going to be a hidden input)
HTML
<form>
<input id="otherAmt" class="qty" type="text" placeholder="Amount Hidden" />
<p> The Costs are:</p> <input type="text" id="otherAmtHidden" name="otherAmtHidden" readonly="" />
</form>
var otherAmt = document.getElementById("otherAmt");
var otherAmtHidden = document.getElementById("otherAmtHidden");
otherAmt.oninput = function(){
alert('input Received');
var val1 = parseInt(otherAmt.value, 10);
alert(val1 + 'val1');
var amtHidden = val1;
alert(amtHidden + 'amtHidden');
//otherAmtHidden.innerHTML = amtHidden;
$('input [name="otherAmtHidden"]').val(amtHidden);
alert('finished');
};
Edit: I'm dumb, Solved it by changing the $('input etc').val(amtHidden); to $('#otherAmtHidden).val(amtHidden);
I'm dumb, I Solved it by changing the $('input etc').val(amtHidden); to $('#otherAmtHidden).val(amtHidden);
Fixed Javascript
var otherAmt = document.getElementById("otherAmt");
var otherAmtHidden = document.getElementById("otherAmtHidden");
otherAmt.oninput = function(){
var val1 = parseInt(otherAmt.value, 10);
var amtHidden = val1;
$('#otherAmtHidden').val(amtHidden);
};
Ok, say I have a checkbox such as this:
<input type="checkbox" value="1" class="discount_select" name="select[101132]">
...and 3 text fields like this:
<input type="text" name="start[101132]">
<input type="text" name="end[101132]">
<input type="text" name="discount[101132]">
I am running some code right now that will update the text field values if the checkbox is checked, however I'm not sure if or how you can target the correct fields as they all have different ID's.
So I basically have this code to loop through the checked boxes, but not sure how to make updates to the correct text fields:
// Get values
var discount = $('#apply_discount').val();
var start = $('#apply_start_date').val();
var end = $('#apply_end_date').val();
$('.discount_select:checked').each(function() {
// How can I target the correct fields/ID's here?
});
Try
// Get values
var discount = $('#apply_discount').val();
var start = $('#apply_start_date').val();
var end = $('#apply_end_date').val();
$('.discount_select:checked').each(function() {
var num = this.name.substring(7, this.name.length - 1);
$('input[name="start[' + num + ']"]').val(start)
$('input[name="end[' + num + ']"]').val(end)
$('input[name="discount[' + num + ']"]').val(discount)
});
Change the name and ids of your fields to make it simpler
<input type="checkbox" value="1" class="discount_select" id="101132" name="select_101132">
<input type="text" name="start_101132">
<input type="text" name="end_101132">
<input type="text" name="discount_101132">
Then:
var discount = $('#apply_discount').val();
var start = $('#apply_start_date').val();
var end = $('#apply_end_date').val();
$('.discount_select:checked').each(function() {
var select_id = this.attr("id");
$('[name=start_'+select_id+']').val(start);
$('[name=end_'+select_id+']').val(end);
$('[name=discount_'+select_id+']').val(discount);
});