Jump to content


Photo

Desabilitar Um Imput Text Através De Chamada De Função Js


  • Faça o login para participar
1 reply to this topic

#1 Lucas99

Lucas99

    Normal

  • Usuários
  • 84 posts
  • Sexo:Masculino
  • Localidade:Santos

Posted 21/01/2015, 23:37

 tenho um input text de telefone e ao lado um checkbox que diz que se o cliente não tiver telefone para marcar e ai eu queria que quando fosse marcado o imput text fosse desabilitado e não pudesse mais ser escrito, é possível?

 


#2 Danilomaru

Danilomaru

    Ativo

  • Moderadores
  • 300 posts
  • Sexo:Masculino
  • Localidade:brasil

Posted 26/01/2015, 00:43

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>

<form action="" id="form">
<input type="text" id="name" />
<input type="checkbox" id="check" />

<input type="submit" />
</form>

<script>
(function(){
var $name = document.getElementById('name'),
$check = document.getElementById('check'),
$form = document.getElementById('form');

$check.addEventListener('click', function() {
if (!!this.checked) {
$name.setAttribute('disabled', 'disabled');
} else {
$name.removeAttribute('disabled');
}
});

$form.addEventListener('submit', function(e) {
e.preventDefault();

alert('Form Submitted');
});

}());
</script>
</body>
</html>

se você marcar o checkbox, não consegue mais escrever no input.
Se desmarcar o checkbox, consegue escrever novamente.






0 user(s) are reading this topic

0 membro(s), 0 visitante(s) e 0 membros anônimo(s)

IPB Skin By Virteq