Change input value with javascript

0

I have a field to insert notes and I would like that if the value entered was greater than 10 or less than 0 the value was erased from the input.

I tried to do with the code below, but without success.

function verifica(v){
  if (v > 10 || v < 0){
	  v = '';
  }
}
<input type="number" onchange="verifica(this.value)">
    
asked by anonymous 17.07.2017 / 22:15

1 answer

2

Pass this as argument of onchange and change method to work with element and not just value.

So you can change the value property.

function verifica(el){
  if (el.value > 10 || el.value < 0){
    el.value = '';
  }
}
<input type="number" onchange="verifica(this)">
    
17.07.2017 / 22:18