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 7869107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T01:12:46+00:00 2026-06-03T01:12:46+00:00

i want define a variable in Json callback. Code; $(select).change(function () { var $variable

  • 0

i want define a variable in Json callback.

Code;

$("select").change(function () {
          var $variable = "";
          $("select option:selected").each(function () {
                $variable += $(this).text() + " ";
              });
          $("div.yaz").text($variable);

      $('#result').html('loading...');

    $.getJSON('program-bilgileri.php', function(JSON){
        $('#result').empty();

        $.each(JSON.$variable, function(i, program){
            $('#result')
            .append(program.isim +'<br />')
            .append(program.bilgi+'<br />')
            .append(program.adres+'<hr />');
      });
    });
    })
    .trigger('change');

program-bilgileri.php returns;

{
   "programlar":[
      {
         "isim":"Zone Alarm",
         "bilgi":"bilgisayarın güvenliğini sağlar",
         "adres":"www.zonealarm.com"
      },
      {
         "isim":"Opera",
         "bilgi":"güvenli ve hızlı bir web tarayıcısıdır",
         "adres":"www.opera.com"
      },
      {
         "isim":"Photoshop",
         "bilgi":"güçlü bir imaj işleme yazılımıdır",
         "adres":"www.adobe.com"
      }
   ]
}

The problem is here “$.each(JSON.$variable, function(i, program)” if I define $variable in JSON it isn’t working.

Any idea?

  • 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. Editorial Team
    Editorial Team
    2026-06-03T01:12:48+00:00Added an answer on June 3, 2026 at 1:12 am

    The problems i see are

    • Inside the change event you are using $("select option:selected") which finds all select elements in the page, and not the changed one only.
      use $(this).children('option:selected') instead.
    • I am assuming that you are allowing multiple selection on the select element and that is why you are doing += with the $variable.. (you are also adding a space at the end). That means, though, that the variable will be something like "programlar " or "programlar somethingelse".
      Your returned JSON though has a key of programlar. A single word, no spaces.. so when you do JSON[$variable] which is the correct way to access an element based on the name in a variable, it does not match.

    If the <select> element does not allow multiple selection then the solution is

    $("select").change(function() {
        var $variable = $(this).children("option:selected").text();
    
        $("div.yaz").text( $variable );
    
        $('#result').html('loading...');
    
        $.getJSON('program-bilgileri.php', function(JSON) {
            $('#result').empty();
            $.each(JSON[$variable], function(i, program) {
                $('#result')
                    .append(program.isim + '<br />')
                    .append(program.bilgi + '<br />')
                    .append(program.adres + '<hr />');
            });
        });
    }).trigger('change');
    

    If indeed it is a multiselect and each option can appear in the JSON then you must check for each option found in the variable.

    $("select").change(function() {
        var $variable = $(this).children("option:selected").map(function(){
                               return $(this).text();
                        }).get();
    
        $("div.yaz").text( $variable.join(' ') );
    
        $('#result').html('loading...');
    
        $.getJSON('program-bilgileri.php', function(JSON) {
            $('#result').empty();
    
            for (index=0, length = $variable.length; index < length; index ++) {
                $.each(JSON[$variable[index]], function(i, program) {
                    $('#result')
                        .append(program.isim + '<br />')
                        .append(program.bilgi + '<br />')
                        .append(program.adres + '<hr />');
                });
            }
        });
    }).trigger('change');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to define a ref type variable, like this: SomeType v=null; and this
How can I define a global javascript variable in this situation? I want the
I want to define some member variable and some code just in Debug Mode,
If I have var doc=window.document; and I don't want to define another variable, how
I want to automatically define incrementing variable names. So instead of this: $Var1 =
I want to define a class method that has access to a local variable.
At first sorry my bad English. I want define routing for this url: For
I want to define a following function: if(stmtToFinalize) { NSLog(@Finalizing statement stmtToFinalize); if (sqlite3_finalize(stmtToFinalize)
I want to be able to define a variable by the return value of
I have a global JSON variable where I store some parameters and then each

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.