Set JS variable in input value HTML

2

I have several inputs in an HTML where the IDs are correct. I want to get value of these Inputs with these JavaScript variables, however only the Input of nome takes the value of the variable! The rest of the inputs are empty:

 $.ajax({
        url: 'api/user/getSession',
        type: 'POST',
        success: function(data){
            document.getElementById("nome").value = data.nome,
            document.getElementById("perfil").value = data.perfil,
            document.getElementById("empresa").value = data.perfil
        }
    });
    
asked by anonymous 20.02.2015 / 17:24

2 answers

3

Your problem is not in the use of jQuery or JavaScript, but in commas (% with% commas), the correct one would be , (semicolon and semicolon), see you did so:

document.getElementById("nome").value = data.nome,
document.getElementById("perfil").value = data.perfil,
document.getElementById("empresa").value = data.perfil

The correct thing is this:

document.getElementById("nome").value = data.nome;
document.getElementById("perfil").value = data.perfil;
document.getElementById("empresa").value = data.perfil;
  

Note that the last semicolon is not necessary, but we usually use it to avoid errors.

In jQuery you also used comma:

$("#nome").val(data.nome),
$("#perfil").val(data.perfil),
$("#empresa").val(data.perfil)

Even though the error does not occur, it is best to use ; :

$("#nome").val(data.nome);
$("#perfil").val(data.perfil);
$("#empresa").val(data.perfil);

Final result:

$.ajax({
    url: 'api/user/getSession',
    type: 'POST',
    success: function(data){
        $("#nome").val(data.nome);
        $("#perfil").val(data.perfil);
        $("#empresa").val(data.perfil);
    }
});
    
25.02.2015 / 19:59
1
$.ajax({
        url: 'api/user/getSession',
        type: 'POST',
        success: function(data){
            $("#nome").val(data.nome),
            $("#perfil").val(data.perfil),
           $("#empresa").val(data.perfil)
        }
    });
    
25.02.2015 / 19:05