Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 143529
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T08:06:36+00:00 2026-05-11T08:06:36+00:00

I know there is a far more elegant/efficient way of doing this (in php

  • 0

I know there is a far more elegant/efficient way of doing this (in php I would use foreach) but with jQuery how can I walk the var/val pairs of a JSON response and populate form fields with the same id’s as the field names in the JSON response?

Here is my JSON response:

[{'field':'svendor_name','value':'Vendor Name Inc.'},{'field':'svendor_addr1','value':'1234 Vendor Lane.'},{'field':'svendor_addr2','value':'Suite 100'},{'field':'svendor_city' ,'value':'Vendorville'},{'field':'svendor_state','value':'CA'},{'field':'svendor_zip','value':'90210'},{'field' :'svendor_phone','value':'800-555-1234'}] 

Here is my jQuery code for populating the form:

$(document).ready(function() {     $('#svendor_name').bind('change', function()     {         var svendor = $('#svendor_name').val();         svendor = svendor.replace(/&/g, '*');         $.getJSON('get_vendors.php?sname=' + svendor,         function(data)         {             $.each(data,                 function(i, item)                 {                     if(item.field == 'svendor_name')                     {                         $('#svendor_name').val(item.value);                     }                     else if(item.field == 'svendor_addr1')                     {                         $('#svendor_addr1').val(item.value);                     }                     else if(item.field == 'svendor_addr2')                     {                         $('#svendor_addr2').val(item.value);                     }                     else if(item.field == 'svendor_city')                     {                         $('#svendor_city').val(item.value);                     }                     else if(item.field == 'svendor_state')                     {                         $('#svendor_state').val(item.value);                     }                     else if(item.field == 'svendor_zip')                     {                         $('#svendor_zip').val(item.value);                     }                     else if(item.field == 'svendor_phone')                     {                         $('#svendor_phone').val(item.value);                     }                     else if(item.field == 'svendor_id')                     {                         $('#svendor_id').val(item.value);                     }             });         });     }); }); 

That all works fine and good but I really want to avoid all the if/else statements and just use the data coming back from the getJSON method to determine what fields get populated with what values. What is a cleaner/more effective approach to this?

— Nicholas

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T08:06:36+00:00Added an answer on May 11, 2026 at 8:06 am

    You can get rid of all ‘if’ statements by replacing your $.each with this:

    $.each(data, function(i, item){   $('#'+item.field).val(item.value); }); 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

As far as I know, there's no way to use {% include %} within
I know there have been a few threads on this before, but I have
I know there is a registry key indicating the install directory, but I don't
I know there are a lot of positive things mod-rewrite accomplishes. But are there
I know there is a way to add a IE control, how do you
I know there are HTML entities for 1/2, 1/4, and 3/4, but are there
I know there are quite a few line count tools around. Is there something
I know there is a function somewhere that will accept a client rect and
I know there's some JAVA_OPTS to set to remotely debug a Java program. What
I know there is a Jackpot API http://jackpot.netbeans.org/docs/org-netbeans-modules-jackpot/overview-summary.html for programmatic access the the rules

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.