This question already has answers here:
How do I get the value of text input field using JavaScript?
(16 answers)
Closed 6 years ago.
I have two input type = range:
<input type="range" id="range_1" name="rangeA">
<input type="range" id="range_2" name="rangeB">
I want to get the values and put them in a function.
How can i do that?
first you need to select the input, thankfully you have separate ids on them,
Vanilla JS:
var element = document.getElementById('range1').innerHTML;
Here is a list of stuff you can then do with that element, like innerHTML
https://developer.mozilla.org/en-US/docs/Web/API/Element
Related
This question already has answers here:
Sort a string alphabetically using a function
(3 answers)
Change input value onclick button - pure javascript or jQuery
(6 answers)
change value of input field onclick
(5 answers)
Closed 3 months ago.
The question is how to sort the letter in alphabetic order based on the input in a HTML input tag, then clicks a button to sort it, after click the button the input will move to a text area and is already sorted when button is click, so that button need to have the insert function and sorting function, now the input can be insert to the textarea but not sorted, thanks.
Example of input:
andwe
output:
adenw
i want to define my input as an element, and write onclick="sortstring(element of my input)" in button, but i dont know how to define and dont have a sort function yet.
function sortString() {
const inputElement = document.getElementById('input');
const sortResult = inputElement.value.split('').sort().join('');
inputElement.value = sortResult;
}
<input id='input' type="text" value=''/>
<button onclick="sortString()">sort</button>
function sortString(str) {
document.querySelector('#result').innerHTML = str.split('').sort().join('')
}
<input type='text' onkeyup='sortString(this.value)'>
<p id="result"></p>
This question already has answers here:
How to Copy Value from one Input Field to Another
(2 answers)
Closed 2 years ago.
Let's say I have two inputs in HTML:
<input type="number" id="test1">
<input type="number" id="test2">
Now I've an algorithm which should be called after enter of some number in input. For example, if I enter 10 in first input, in twice input must be returned 5 (10/2) without click of any button. So how to archive that?
You can use the input event listener.
document.getElementById("test1").addEventListener("input", function() {
document.getElementById("test2").value = this.value/2;
})
This question already has answers here:
Putting text in a number input doesn't trigger change event?
(3 answers)
Closed 5 years ago.
I have an Input field in my form. I want to be able to past only numbers in this input field.
For example: (Some random characters on my clipboard W123W000)
Should Paste = 123000
Note: Only works in Chrome Browser
I have been searching online and so far I came up with this but it's not working properly.
var inputBox = document.getElementsByClassName('numbersOnly');
inputBox.onchange = function () {
inputBox.value = inputBox.value.replace(/[^0-9]/g, '');
}
<input id="number" type="number" class="numbersOnly">
Use jQuery for cross-browser compatibility and bind the event input to handle every change.
Firefox: <input type="number">
elements automatically invalidate any entry that isn't a number (or empty, unless required is specified).
You can change to type=text, but you will lose the numeric keyboard behavior in devices.
var inputBox = $('.numbersOnly').on('input', function(e) {
e.preventDefault();
$(this).val($(this).val().replace(/[^0-9]/g, ''));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="number" type="text" class="numbersOnly">
This question already has answers here:
Get the value in an input text box
(13 answers)
Closed 8 years ago.
I am very new to programing so sorry if this question is to vague.
I have a simple html input field:
input class="draft" type="text"
and I have a javascript function (the function only accepts strings):
Message.send()
Using javascript/jQuery how do I take the string the user typed into the input field and place it into the Message.send() function (in string form)?
Thanks in advance for any help
jQuery:
Message.send($("input.draft").val());
javascript:
Message.send(document.querySelector("input.draft").value);
No jQuery needed. I'm not sure what you mean by place it into the Message.send() function (in string form), so I assume you get the value in the text input and use it as an argument in the Message.send() function.
Try this:
Message.send(document.getElementsByClassName("draft")[0].value);
Assuming your input field is something like this:
<input type="text" id="someInputField" name="someInputField" class="inputFields" />
You could do this
<script type="text/javascript">
//We use the field's id to refer to it and get to its value (the text in it):
var message = $('#someInputField').val();
//And then you might call the function like:
nameOfTheFuntion(message);
</script>
You would need to have jQuery libraries to make it work though, but you could do without them by replacing:
$('#someInputField').val();
with
document.getElementById('someInputField').val();
Give your <input> box an id for example
<input type="text" id="blah" />
In your javascript you are able to reference the <input> like so:
var strBlah = document.getElementById("blah").value;
Now you have the value typed into the <input> box so you would do the following:
Message.send(strBlah)
This question already has an answer here:
JQuery Validate: How do I add validation which checks the sum of multiple fields?
(1 answer)
Closed 8 years ago.
I'm working on a form where the user enters a total, then enters more values into other fields that represent a dividing up of that total. For example:
<input type="text" name="total" />
<input type="text" name="portion[1]" />
<input type="text" name="portion[2]" />
<!-- and so on -->
<input type="text" name="portion[n]" />
If the user enters 123.45 into total, then they need to fill out the portions 1 - n such that their values add up to 123.45. Each portion field is required to be a positive number or 0 but those are the only other restrictions on them.
The jquery.validate plugin has an equalTo validation method, but this can only seem to cope with a single field, rather than a set.
Is there a way to
Define a validation rule that will validate the total of the group of fields against the total field
Get a single message to display for the group of fields if they don't add up
Try this function with a jquery event
function Mvalidate()
{
var total=$('[name=total]').val();
var n=10; // no of portions
var partialsum=0;
for(var i=0;i<n; i++)
{
var t=$("[name=portion["+i+"]]").val();
partialsum+=parseFloat(t);
}
if(partialsum<total)
alert("Portions add up not complete!");
}
$("#checkbutton").click(function()
{
Mvalidate();
});
#Krishnan:
according Jquery Doc the
$('.total').
is a class selector, isn't it? If you want to look for an element with a name attribute, you have to write it as follow:
$("[name='total']").val();
$("[name='portion["+i+"]']").val();
If you know there is only an input field with that name, you can use
$("input[name='total']").val();
This question looks like it has some answers that could be useful in solving this problem.