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

  • Home
  • SEARCH
  • 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 call $(form[name='searchbox-purchase'] select[name='city']).change(function(){ }); i have 4 forms with following names searchbox-purchase searchbox-rental
jquery Ui AutoComplete has a property that named Source. i want to value this
jQuery is really easy for creating a nice 'edit in place' effect, but, despite
jQuery.parseJSON('{"name":"John"}') converts string representation to object but I want the reverse. Object is to
jQuery: $(#contact-us-form).submit(function () { var nameVal = $(input[name=name]).val(); var companyVal = $(input[name=company]).val(); var titleVal
jQuery 1.4.2 omits the timestamp GET parameter (to defeat browser cacheing) if I assert
jQuery was updated over the weekend. How long should I expect to wait before
jQuery plugins use a pattern like this to hide private functions of a plugin:
jQuery trim not working. I wrote the following command to remove white space. Whats
jQuery plugins make code much more reusable. I previously made a plugin that would

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.