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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:35:33+00:00 2026-05-22T01:35:33+00:00

In jQuery, I’m creating select boxes on the fly based on the number of

  • 0

In jQuery, I’m creating select boxes on the fly based on the number of items in a PHP array, and attempting to change the value of each select box option to the value of the array item.

First, here’s the HTML:

<div id="artistArea">
  <div class="category_block">
    <select name="artistSelect[]" class="cat_list">
        <option value="">- none -</option> 
    </select>
  </div>
</div>

Here’s the array conversion:

var arrayFromPHP = <?php echo json_encode($exhibArray) ?>; // There's our array from php->jquery

I then start by populating the options in the first select and then create the subsequent boxes within an ‘each’ loop in the ‘success’ callback:

$.ajax({
    url: 'artistpop.php',
    success: function(data){
        $('.cat_list').append(data); //Sets options for first select box
        $.each(arrayFromPHP, function (i, elem) { //loop through our array values
            $("#artistArea > .category_block:last .cat_list").val(elem); //set the select box value
            $('#artistArea > .category_block:last').after($('#artistArea > .category_block:last').clone()); //clone the previous select box     
        });
    }   
});

So… The problem is:

The initial box creation and population is working fine, as is the creation of the subsequent boxes. However, only the first select box is set with the appropriate value from the array; the others remain on the ‘none’ option.

Could this be something wrong in the each loop?

I hope this is clear enough to understand – I’m confused!

  • 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-05-22T01:35:34+00:00Added an answer on May 22, 2026 at 1:35 am

    Have you tried setting a “selected” attribute on the actual option you want as your default, rather than setting the value of the select? In other words, something like…

    $('#artistArea > .category_block:last .cat_list option[value=' + elem + ']').attr('selected','selected');

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

jQuery(document).ready(function(){ $(function() { $('.button-a').each(function() { $(this).hover( function () { $('.button-title', this).animate({ opacity: 0.6 },
jQuery.unique lets you get unique elements of an array, but the docs say the
jQuery(function() { jQuery(ul.logos-sprite-icon-wrap li a.logos-icon).hover(function() { jQuery(this).animate({ 'padding-top': '0px', 'padding-bottom': '5px', }, 500); },
Jquery and other JS frameworks got many lazy image loading plugins, code-blocks. Which basically
jQuery 1.4 added a shorthand way for constructing new DOM Elements and filling in
Jquery Source : $.ajax({ type: 'get' , dataType: 'text' , url: 'navigation.html' , cache:
Jquery UI autocomplete wont work,what could be wrong?Here is the code.Thank you <head> <meta
Jquery provides a very convenient way to delay executing code until the DOM is
jQuery's effects are great, but I've noticed that the remaining elements under the affected
jQuery File Uploader: https://github.com/blueimp/jQuery-File-Upload I'm using the plugin above. How in jQuery can I

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.