Stan Williams Stan Williams - 1 month ago 10
Javascript Question

Show a form filled with Javascript variables in jquery mobile

enter image description hereI have a page which takes data from a mysql database and exports it as JSON and then displays the data on a page using javascript.

That all works fine, but i also want to be able to edit the data, and so want to be able to display a form which is filled with the javascript variables generated from the json output from mysql. So instead of just showing the table of results, i want to show a form pre-filled with the current values from the json output.

I have tried several ways to display the form elements, using the method below it shows the code, but does display the name, if i take the input field out of the single quotes, nothing is displayed.

So the picture shows the information retrieved from the database but i would like to show the information in a form.

function displayEmployee1(data) {
var employee = data.item;
console.log(employee);
$('#fullName').text('<input name="name" type="text" placeholder="Name" value="' + employee.number + '"/>');
console.log(employee.telephone);

Answer

You are trying to insert html, so change:

$('#fullName').text(...

to:

$('#fullName').html(...

(Or rather than creating input elements on the fly I would probably create the entire form directly in the html and then just use JS to populate it with the values from the JSON, using $("input[name='name']).val(employee.name), etc.)