Define value min and max of newly created input in javascript

3

I create an input in the following way:

var monto_inp = document.createElement("input");
monto_inp.name = "monto";
monto_inp.placeholder = "Monto";
monto_inp.classList.add('detalle_pago');
monto_inp.classList.add('monto');

I would like to add a minimum and maximum value with min and max, try with monto_inp.min but it did not work.

    
asked by Kinafune 01.09.2018 в 13:05
source

1 answer

4

In javascript , to modify the attributes of an html element or add-you must use the function setAttribute(name, value); :

  

Sets the value of an attribute in the indicated element. If he   attribute already exists, the value is updated, otherwise, the   New attribute is added with the name and value indicated.

Syntax

Element.setAttribute(name, value);

name

  

A DOMString indicating the name of the attribute whose value is going to   change. The name of the attribute is automatically converted to   lowercase when setAttribute () is called over an HTML element in a   HTML document.

value

  

A DOMString that contains the value to assign to the attribute. Any   indicated value that is not a text string becomes   automatically in a text string.

Documentation.

Keeping in mind the documentation, try this:

    var monto_inp = document.createElement("input");
    monto_inp.setAttribute("type","number");
    monto_inp.setAttribute("max",100);
    monto_inp.setAttribute("min",2);
    
answered by 01.09.2018 / 13:12
source