4
Guys I have 2 input:
<input name='nome1'>
<input name='nome2'>
I need to create a javascript that clears Nome2 when the value of name1 is modified.
Can someone help me make it very simple?
4
Guys I have 2 input:
<input name='nome1'>
<input name='nome2'>
I need to create a javascript that clears Nome2 when the value of name1 is modified.
Can someone help me make it very simple?
5
That’s simple:
var nome1 = document.querySelector('[name="nome1"]');
var nome2 = document.querySelector('[name="nome2"]');
nome1.addEventListener('change', function() {
nome2.value = '';
});
That way when the input changes, the nome2
is deleted. If you want you can also do on keyup
or another event depending on the functionality you want to implement.
2
See the example using jQuery. I used keyup
in the event, that is, the function will be invoked each time you enter something in the input.
A tip, prefer to use
id
for the inputs instead ofname
, forid
should always be unique, already thename
no, and it gets easier to manipulate using jQuery as well.
var $nome1 = $('input[name=nome1]');
var $nome2 = $('input[name=nome2]');
$nome1.on('keyup', function() {
$nome2.val('');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input name='nome1'>
<input name='nome2'>
Very good was just what I needed ;)
Browser other questions tagged javascript jquery html
You are not signed in. Login or sign up in order to post.
Very good was just what I needed ;)
– Hugo Borges