I am trying to find out how to set the max value and min value of html5 type input by javascript or jquery.
<input type="number" max="???" min="???" step="0.5"/>
Would someone please guide
The min attribute specifies the minimum value for an <input> element. Tip: Use the min attribute together with the max attribute to create a range of legal values. Note: The max and min attributes works with the following input types: number, range, date, datetime-local, month, time and week.
addEventListener('change', function(e) { var num = parseInt(this. value, 10), min = 0, max = 100; if (isNaN(num)) { this. value = ""; return; } this.
The HTML <input> tag is used to get user input in HTML. To give a limit to the input field, use the min and max attributes, which is to specify a maximum and minimum value for an input field respectively.
jQuery makes it easy to set any attributes for an element - just use the .attr()
method:
$(document).ready(function() { $("input").attr({ "max" : 10, // substitute your own "min" : 2 // values (or variables) here }); });
The document ready handler is not required if your script block appears after the element(s) you want to manipulate.
Using a selector of "input"
will set the attributes for all inputs though, so really you should have some way to identify the input in question. If you gave it an id you could say:
$("#idHere").attr(...
...or with a class:
$(".classHere").attr(...
Try this:
<input type="number" max="???" min="???" step="0.5" id="myInput"/> $("#myInput").attr({ "max" : 10, "min" : 2 });
Note:This will set max and min value only to single input
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With