How to add value to an input when pressing a link with jquery?

1

I'm having the following problem: I'm not able to set the value to a input when it's inside a function . Example:

$(document).on('click',".edita_cliente", function(){
    var id = $(".seq").val();
    $.ajax({
        url: 'php/lista_clientes.php?service=2&id=' + id,
        type: 'get',
        dataType: 'json',
        success: function (data) {
            setTimeout(function(){
                $("#content").load('cadastra_cliente.php');
                $("#nome_cad").val(data[0].Nome);
            }, 700);
        }
    });
});
  

This function, when I click the button, it closes the modal, loads the   page. But it is not putting the value in input . I tried other   forms, and I noticed that it does not set any value in input when it is   within function . Can anyone explain me why?

     

Thank you in advance!

    
asked by anonymous 14.12.2017 / 00:21

1 answer

0

You need to make a callback in the .load to be able to enter the value in the input that will be loaded. This setTimeout is totally unnecessary. See:

$(document).on('click',".edita_cliente", function(){
   var id = $(".seq").val();
   $.ajax({
      url: 'php/lista_clientes.php?service=2&id=' + id,
      type: 'get',
      dataType: 'json',
      success: function (data) {
         $("#content").load('cadastra_cliente.php', function(){
            $("#nome_cad").val(data[0].Nome);
         });
      }
   });
});

After the callback , the element will be properly loaded in .load and can be used. In the way you're doing, the code goes straight from .load without waiting for it to return.

    
14.12.2017 / 00:40