Problem retrieving 2 values with javascript and jquery

0

I'm having trouble retrieving 2 values and sending through load. I can retrieve the link data from the 'date' attribute but the input I can not retrieve to concatenate with the 'date' and do the right path. Follow the code.

Javascript:

$('.buscarr').click(function() {
    var id = $(this).attr('campo1');
    var id2 = $(this).attr('data');
    alert(id);
    console.log(id);
    console.log(id2);
    $("#cliente1").load($(this).attr(id2 + id));
});

HTML:

<div class="form-group">
    <label for="busca">Nome Servico*:</label> <input id="campo1" type="text" class="form-control" placeholder="Insira um nome para busca">
</div>
<a href="#" data="/wbahd/servico_servlet?acao=buscar&busca=" class="btn btn-default btn-cadastrar-btn buscarr">Buscar</a>

</div>
    
asked by anonymous 18.05.2017 / 23:48

1 answer

0

Returns nothing because attr searches for an element attribute, and fetching does not. - Lucas Costa

Use var id = var id = $('#campo1').val(); or var id = $('form-control').val();

$(document).ready(function(){
$('.buscarr').click(function() {
    var id = $('#campo1').val();
    //var id = $('form-control').val();
    var id2 = $(this).attr('data');
    //alert(id);
    console.log(id);
    console.log(id2);
    //$("#cliente1").load($(this).attr(id2 + id));
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script><divclass="form-group">
    <label for="busca">Nome Servico*:</label> <input id="campo1" type="text" class="form-control" placeholder="Insira um nome para busca">
</div>
<a href="#" data="/wbahd/servico_servlet?acao=buscar&busca=" class="btn btn-default btn-cadastrar-btn buscarr">Buscar</a>

</div>

You can also use var id = document.getElementById('campo1').value;

$(document).ready(function(){
$('.buscarr').click(function() {
    var id = document.getElementById('campo1').value;
    var id2 = $(this).attr('data');
    //alert(id);
    console.log(id);
    console.log(id2);
    //$("#cliente1").load($(this).attr(id2 + id));
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script><divclass="form-group">
    <label for="busca">Nome Servico*:</label> <input id="campo1" type="text" class="form-control" placeholder="Insira um nome para busca">
</div>
<a href="#" data="/wbahd/servico_servlet?acao=buscar&busca=" class="btn btn-default btn-cadastrar-btn buscarr">Buscar</a>

</div>
    
19.05.2017 / 01:44