Change input value with javascript

Asked

Viewed 5,713 times

0

I have a field to insert notes and I would like if the entered value was greater than 10 or less than 0 the value was deleted 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)">

1 answer

2


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

So you can change the property value.

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.