Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the max value and min value of <input> in html5 by javascript or jquery? [closed]

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

like image 711
user2625363 Avatar asked Oct 11 '13 11:10

user2625363


People also ask

How do you set a minimum and maximum value for an input element in html5?

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.

How do you set the maximum and minimum value of an input type text?

addEventListener('change', function(e) { var num = parseInt(this. value, 10), min = 0, max = 100; if (isNaN(num)) { this. value = ""; return; } this.

How do you limit input value in HTML?

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.


2 Answers

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(... 
like image 89
nnnnnn Avatar answered Sep 23 '22 12:09

nnnnnn


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

like image 37
Dhaval Bharadva Avatar answered Sep 20 '22 12:09

Dhaval Bharadva