简体   繁体   中英

How Do I dynamically populate JSON data on html input field using javascript?

how do i populate these following JSON data on a html input field on the screen ? this is the json arraylist

[{
    "Bank Account Name": "State Bank",
    "Currency Code": "4000",
    "Deposit Date": "5/2/1794",
    "Payment Channel": "check",}]

i have stored this above data in a JSON file how do i get it on the screen?

<input type="text" id="BankAccountName" />
<input type="text" id="CurrencyCode" />
<input type="text" id="DepositDate" />
<input type="text" id="PaymentChannel" />

This is a very basic way of populating input fields with the relevant values

var jsonData = [{
    "Bank Account Name": "State Bank",
    "Currency Code": "4000",
    "Deposit Date": "5/2/1794",
    "Payment Channel": "check",}]

$("#BankAccountName").val(jsonData[0]['Bank Account Name']);
$("#CurrencyCode").val(jsonData[0]['Currency Code']);
$("#DepositDate").val(jsonData[0]['Deposit Date']);
$("#PaymentChannel").val(jsonData[0]['Payment Channel'])

DEMO

Note: You have not referred how this json is loading. Is it in an external file or it is supplied through an api.

Another approach:

var json = [{
        "Bank Account Name": "State Bank",
        "Currency Code": "4000",
        "Deposit Date": "5/2/1794",
        "Payment Channel": "check",}]

Object.keys(json[0]).map(value => {
  document.getElementById(value.replace(/ /g, "")).value = json[0][value]
})

The benefits are you won't need to change anything if you will add some new fields to JSON.

You can try below code:

 var data = [{ "Bank Account Name": "State Bank", "Currency Code": "4000", "Deposit Date": "5/2/1794", "Payment Channel": "check", }]; $(document).ready(function() { var jsonObj = data[0]; for (var key in jsonObj) { if (jsonObj.hasOwnProperty(key)) { $("#" + key.replace(/ /g, "")).val(jsonObj[key]); } } }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <input type="text" id="BankAccountName" /> <input type="text" id="CurrencyCode" /> <input type="text" id="DepositDate" /> <input type="text" id="PaymentChannel" /> 

 var myvariable = [{"id":"15aea3fa","firstname":"John","lastname":"Doe"}]; $('#mything').text('id:'+myvariable[0].id+' name:'+myvariable[0].firstname+' '+myvariable[0].lastname); var value=[{ "BankAccountName": "State Bank", "CurrencyCode": "4000", "DepositDate": "5/2/1794", "PaymentChannel": "check",}]; $('#BankAccountName').val(value[0].BankAccountName); $('#CurrencyCode').val(value[0].CurrencyCode); $('#DepositDate').val(value[0].DepositDate); $('#PaymentChannel').val(value[0].PaymentChannel); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> processed:<div id='mything'></div> <input type="text" id="BankAccountName" /> <input type="text" id="CurrencyCode" /> <input type="text" id="DepositDate" /> <input type="text" id="PaymentChannel" /> 

there you go, a quick jquery solution.

var json = [{
    "Bank Account Name": "State Bank",
    "Currency Code": "4000",
    "Deposit Date": "5/2/1794",
    "Payment Channel": "check"}];

$.each(json[0] , function (key, value){
    key = key.replace(/ /g,'');
    $('input[id='+key+']').val(value);
});

You can iterate through json data and set the value of text field. `

 var data= [{
    "Bank Account Name": "State Bank",
    "Currency Code": "4000",
    "Deposit Date": "5/2/1794",
    "Payment Channel": "check",}];

$.each(data[0],function(key,value){
id=key.replace(" ","");


document.getElementById(id).value=value;
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM