2
I have the following doubts: 1- In my code you have two options (CPF/CNPJ) and the options only appear when you click on one of the two, I wonder how I can do so that the Cpf option first appears active? 2-How can I put "required" and "autofocus" in the fields only when they are active and when hidden do not have these resources? I appreciate anyone who can help. Follows my code:
$('#radioBtn a').on('click', function() {
var sel = $(this).data('title');
var tog = $(this).data('toggle');
$('#' + tog).prop('value', sel);
$('a[data-toggle="' + tog + '"]').not('[data-title="' + sel + '"]').removeClass('active').addClass('notActive');
$('a[data-toggle="' + tog + '"][data-title="' + sel + '"]').removeClass('notActive').addClass('active');
})
jQuery(document).ready(function() {
$("#info2").hide();
$(".inf").click(function() {
var nodo = $(this).attr("href");
if ($(nodo).is(":visible")) {
//$(nodo).hide();
return false;
} else {
$(".oculto").hide("slow").find(':input').prop('disabled', true);
$(nodo).fadeToggle("fast").find(':input').prop('disabled', false);
return false;
}
});
});
//CAMPO CPF
function inpMask(){
var phones = [{ "mask": "###.###.###-##"}];
$('#cpf').inputmask({
mask: phones,
greedy: false,
definitions: { '#': { validator: "[0-9]", cardinality: 1}} });
}
$(window).load(inpMask);
$(document).on("click", ":submit", function(){
var cpf = $("#cpf");
if($(cpf).is(":visible") && $(cpf, "form").val() == ""){ $(cpf).inputmask('remove');
}
$(cpf).keyup(inpMask);
});
//FIM CAMPO CPF
//CAMPO CNPJ
function inpMask(){
var phones = [{ "mask": "##.###.###/####-##"}];
$('#cnpj').inputmask({
mask: phones,
greedy: false,
definitions: { '#': { validator: "[0-9]", cardinality: 1}} });
}
$(window).load(inpMask);
$(document).on("click", ":submit", function(){
var cnpj = $("#cnpj");
if($(cnpj).is(":visible") && $(cnpj, "form").val() == ""){
$(cnpj).inputmask('remove');
}
$(cnpj).keyup(inpMask);
});
//FIM CAMPO CNPJ
#radioBtn .notActive {
color: #3276b1;
background-color: #fff;
}
<link href="//netdna.bootstrapcdn.com/bootstrap/3.1.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<script src="//netdna.bootstrapcdn.com/bootstrap/3.1.0/js/bootstrap.min.js"></script>
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="https://rawgit.com/RobinHerbots/Inputmask/3.x/dist/jquery.inputmask.bundle.js"></script>
<!--TIPO DE CLIENTE-->
<h4><b>Tipo de Cliente:<h4>
<div class="row">
<div class="col-md-4" >
<div class="input-group">
<div id="radioBtn" class="btn-group">
<a class="btn btn-primary btn-sm active inf" href="#info1" data-toggle="happy" data-title="cpf">CPF</a>
<a class="btn btn-primary btn-sm notActive inf" href="#info2" data-toggle="happy" data-title="cnpj">CNPJ</a>
</div>
</div>
</div>
</div>
<!--FIM TIPO DE CLIENTE-->
<!-- COLUNA CPF-->
<div class="row oculto" id="info1">
<!--Campo Nome-->
<div class="col-md-4 col-xs-4">
<div class="form-group">
<label for="nome">Nome:</label>
<div class="input-group">
<div class="input-group-addon">
<span class="glyphicon glyphicon-user" id="basic-addon-nome"></span>
</div>
<input type='text' name='nome' id="id_nome" class="form-control" required autofocus placeholder='Ex.: José Emanoel' onkeyup="limite_nome_fantasia(this)"><br>
</div>
</div>
</div>
<!--Fim Campo Nome-->
<!--Campo CPF-->
<div class="col-md-3 col-xs-4">
<div class="form-group">
<label for="cpf">CPF:</label>
<div class="input-group">
<div class="input-group-addon">
<span class="glyphicon glyphicon-info-sign" id="basic-addon-cpf"></span>
</div>
<input type='text' name='cpf' id="id_cpf" class="form-control" maxlength='30' required autofocus placeholder='Apenas Números' onkeyup="limite_cpf(this)"><br>
</div>
</div>
</div>
<!--Fim Campo CPF-->
</div>
<!--FIM COLUNA CPF-->
<!-- COLUNA CNPJ-->
<div class="row oculto" id="info2">
<!--Campo RAZAO_SOCIAL-->
<div class="col-md-4 col-xs-4">
<div class="form-group">
<label for="razao_social">Razão Social:</label>
<div class="input-group">
<div class="input-group-addon">
<span class="glyphicon glyphicon-user" id="basic-addon-razao_social"></span>
</div>
<input type='text' name='razao_social' id="id_razao_social" class="form-control" required autofocus placeholder='Ex.: José Emanoel' onkeyup="limite_nome_fantasia(this)"><br>
</div>
</div>
</div>
<!--Fim Campo RAZAO_SOCIAL-->
<!--Campo NOME_FANTASIA-->
<div class="col-md-3 col-xs-4">
<div class="form-group">
<label for="razao_social">Nome Fantasia:</label>
<div class="input-group">
<div class="input-group-addon">
<span class="glyphicon glyphicon-user" id="basic-addon-nome_fantasia"></span>
</div>
<input type='text' name='razao_social' id="id_razao_social" class="form-control" required autofocus placeholder='Ex.: José Emanoel' onkeyup="limite_nome_fantasia(this)"><br>
</div>
</div>
</div>
<!--Fim Campo NOME_FANTASIA-->
<!--Campo CNPJ-->
<div class="col-md-3 col-xs-4">
<div class="form-group">
<label for="razao_social">CNPJ:</label>
<div class="input-group">
<div class="input-group-addon">
<span class="glyphicon glyphicon-info-sign" id="basic-addon-cpf"></span>
</div>
<input type='text' name='cnpj' id="id_cnpj" class="form-control" maxlength='30' required autofocus placeholder='Apenas Números' onkeyup="limite_cnpj(this)"><br>
</div>
</div>
</div>
<!--Fim Campo CNPJ-->
</div>
<!--FIM COLUNA CNPJ-->
Perfect! Thank you very much. I have another question. I will edit the question for you to understand.
– Carlos
How can I do when this element is disabled can also be cleaned if there is something previously written?
– Carlos