I recommend the use of checkbox
to say whether it is anonymous or not:
Javascript:
$(function() {
var $check = $("#checkAnonimo");
$check.on("change", function () {
var anonimo = $check.is(":checked");
anonimo ? $("#camposAnonimo").hide() : $("#camposAnonimo").show();
$("#camposAnonimo input").prop("disabled", anonimo);
});
});
Note that in addition to hiding address-related inputs, I also disable them, because even hidden, if you submit the form, the data will be sent if they have been filled in previously.
Html:
<form action="" method="POST">
<div><label for="textOpiniao">Escreva sua opinião:<br/></label><textarea id="textOpiniao">Sua opinião aqui!</textarea></div>
<label for="checkAnonimo"><input type="checkbox" id="checkAnonimo"/> Anônimo?</label>
<div id="camposAnonimo">
<div><label for="textNome">Nome: <input type="text" name="nome" id="textNome"/></label></div>
<div><label for="textEndereco">Endereço: <input type="text" name="endereco" id="textEndereco"/></label></div>
</div>
<div><input type="submit" value="Enviar"/></div>
</form>
Could put the code already developed.
– rray
Check out the guide [Ask]. If by chance you read English, here is a great article on how to improve our write questions.
– brasofilo