1

I'm trying to append values to a SELECT box, that gets dynamically added to the body of a page. A fiddle can be seen here: https://jsfiddle.net/mr2qtmw9/

HTML:

<body>

foo bar

</body>

JS:

var countries = new Object();
countries['AT'] = "Austria";
countries['AU'] = "Australia";
countries['BE'] = "Belgium";
countries['BG'] = "Bulgaria";
countries['CA'] = "Canada";

$(document).ready(function() {

    $('body').append( "<select id='foo'></select>" );

  // fill out the country, and its selected value
  $.each(countries, function(i,x) {
    $('#Country').append($('<option>', { value : i }).text(x));
  });

});

The SELECT box gets created, and the $.each() runs through... but the option's don't get added.

What am I missing? (the same code works fine when the select box appears on the initial DOM... it just seems to have an issue when its dynamically injected)

UPDATE: Sorry, I put the wrong ID in the example! On the live script, the ID is "Country", and the selector is "#Country". It still doesn't work on my dev server (much more complex code), so I guess the issue must lie elsewhere :(

UPDATE 2: Doh, this was a big oversight on my part! (sometimes writing up and issue helps you to spot these). I put an alert() inside the function that was doing the work, and found it was running 2 times. Sure enough, after checking the code I found there was a 2nd place I was calling the same function. As such, it WAS adding the options, but to the 1st instance of the SELECT (and the 2nd one was overlapping, so I couldn't see the first one).

I have half a mind to delete this question, as it makes me look so dumb... but I think I'll leave it up, so hopefully it can help save someone else from a similar embarrassment ;)

6
  • 1
    Wrong id, that's all... Commented Dec 10, 2016 at 9:58
  • $('#Country') !== $('#foo') Commented Dec 10, 2016 at 10:00
  • Thanks guys - that was a boo-boo my end. The actual code has them set correctly. The jsfiddle runs fine now, but my script doesn't (on the server). I'll keep digging Commented Dec 10, 2016 at 10:02
  • @AndrewNewby, do you have errors in console ? Commented Dec 10, 2016 at 10:03
  • Both answer are correct...please approve any one so it will be helpfull to others Commented Dec 10, 2016 at 10:05

2 Answers 2

2

You had the id foo on the select but you were using Country when appending the option. This should work:

var countries = new Object();
countries['AT'] = "Austria";
countries['AU'] = "Australia";
countries['BE'] = "Belgium";
countries['BG'] = "Bulgaria";
countries['CA'] = "Canada";

$(document).ready(function() {

	$('body').append( "<select id='Country'></select>" );

  // fill out the country, and its selected value
  $.each(countries, function(i,x) {
    $('#Country').append($('<option>', { value : i }).text(x));
  });

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>

foo bar

</body>

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks - that was just me being silly on my jsfiddle. The weird thing, is that that DOES work, yet on my dev site it's not (the code is more complex there, which is why I simplified). Guess I need to did a bit deeper to see whats going on :(
I'm glad it worked somehow. But you should post another question maybe with another/more code. Maybe someone can help you. Maybe you forgot to include jQuery on your server.
I'm going to accept this one, purely because it was posted 1 minute before the other answer. While it didn't actually "fix" the real issue I was having, it was correct with regards to my boo-boo in the JSFiddle ;)
1

Just need to change id to foo when you append

var countries = new Object();
countries['AT'] = "Austria";
countries['AU'] = "Australia";
countries['BE'] = "Belgium";
countries['BG'] = "Bulgaria";
countries['CA'] = "Canada";

$(document).ready(function() {

	$('body').append( "<select id='foo'></select>" );

  // fill out the country, and its selected value
  $.each(countries, function(i,x) {
    $('#foo').append($('<option>', { value : i }).text(x));
  });

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<body>

foo bar

</body>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.