Alberto
Alberto

Reputation: 223

How to change the max value for input type number on HTML5 with Javascript?

I need change the max attribute of an input element with JavaScript. For example:

<input type="number" name="miname" min="0" max="MyVar" value="0"/>

Upvotes: 12

Views: 26896

Answers (2)

erickyi2006
erickyi2006

Reputation: 226

sharing the angular way of doing it. Use [attr.max].

<input matInput type="number" min="0"  [attr.max]="totalOrders"  placeholder="Total Skybridge Orders" formControlName="totalSkybridge" (change)="onTotalSkybridgeOrders($event)"

Upvotes: 0

Ibu
Ibu

Reputation: 43810

You can assign an id to your input :

<input id="myInput" type="number" name="miname" min="0" max="MyVar" value="0"/>

then access it via javascript:

var input = document.getElementById("myInput");
input.setAttribute("max",100); // set a new value;

Note you can also access it directly if your input is in a form

document.formName.miname.setAttribute("max",newValue);

Upvotes: 24

Related Questions