Set JS variable in input value HTML

Asked

Viewed 8,009 times

3

I have several inputs in an HTML, where the Ids are correct. I want to search the value of these inputs with these Javascript variables, but only the nome takes the variable value! 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
        }
    });
  • Be clearer, once I understand what you want to do; and to be clearer, put at least the html of your pseudo form .

  • 2

    If you are using the jQuery, why use getElementById? It is easier to use the selector $('#nome').val(data.nome);

  • where the inputs?

  • Pke does not place a console.log(data.d) and see if the result is coming correctly. or even an Alert.

  • The inputs are text? values are coming up on the date variable? to check send a console.log(data.profile)

  • It was already arriving ok! Thank you all and Wallace Maxters worked out yours. I am n00b in JS and jQuery

  • @Danielamarquesdemorais welcome to Sopt, glad your question was answered, it would be interesting to record your experience, put your html code and js, explain your question, as soon as someone offers a solution and is valid to your problem, accept !

  • @Danielamarquesdemorais You can also use $.each in your json return -> $.each(data,function(chave, valor) { $('#' + chave).val(valor);
}); (Just a hint even..., in your case you use a key for 2 different fields, then would need some changes even...)

  • Are the ids correct? The value 'data.profile' is returning value?

  • @deFreitas Yes. The solution was jQuery, thank you

Show 5 more comments

2 answers

4


Your problem is not with jQuery or Javascript, but with commas (, commas), the correct would be ; (point and comma - semicolon), see you did so:

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

What’s right is this:

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

Note that the last point and comma is not necessary, but we use to avoid mistakes.

In jQuery you also wore comma:

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

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

$("#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);
    }
});
  • 1

    Thank you, so obvious and NO one has observed it but you. Enlightening!

1

$.ajax({
        url: 'api/user/getSession',
        type: 'POST',
        success: function(data){
            $("#nome").val(data.nome),
            $("#perfil").val(data.perfil),
           $("#empresa").val(data.perfil)
        }
    });
  • I can’t understand how this code works and other not, you just changed from DOM API to jQuery. Could explain the difference?

  • I have no idea, being that it is practically the same thing but only funfou thus.

  • 1

    I have an Answer for you!

  • Daniela, you erased the other question, about the button that ran a alert unexpected, I was going to ask you a question, when Backbone renders your template it generates more than one <button>?

  • Displayed only the buttons belonging to the page (recover password and save). I didn’t understand how and why but ended up entering the wrong event.

  • 1

    That’s not what I meant, what I mean is that Ids cannot repeat, if the button appears more than once, are generated Repeated Ids and this may have triggered the problem.

  • Probably! I restored the question to have this answer there.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.