Jc John Jc John - 2 months ago 9
Javascript Question

ajax passing the json value to html input type text

I want to pass the output of my ajax json to my input type textbox. How can i do it. I have this code but my code doesn't work. here it is

my script :

function getUserDetails(id) {
$.ajax({
url : "<?php echo site_url('manager/profile/userDetails/')?>/" + id,
type: "GET",
dataType: "JSON",
success: function(data) {
$('#fname').html(data['user_fname']);


},
error: function (jqXHR, textStatus, errorThrown) {
//alert('Error get data from ajax');
}
});


my html input type tag :

<input type="text" class="form-control" id="fname" placeholder="First Name">

Answer

You need to use .val() to add the value into the input. Try the following:

 $('#fname').val(data['user_fname']);

also you need to ensure that that the input can handle this value and and is expecting.