1
<!DOCTYPE html>
<html>
    <script type="text/javascript" src="../jquery-1.4.4.min.js" />
    <script type="text/javascript">        
        function click(){
            var select = document.getElementById("select");
            var json = [["Deutsch", "de-DE"], ["US Englisch", "en-US"]];
            json = JSON.parse(json);
            select.options.length = 0;
            for(var i=0; i<json.length; i++) {
                $("#select").append(new Option(json[i][0], json[i][1], false, false));
            }
        }   
    </script>

    <body>
        <select id="select"></select>
        <input type="button" onclick="click()" />
    </body>
</html>

I'm trying to add options to the select element, but to no avail.

1
  • since you're really not using jQuery except for that one line, why not go straight JS? Commented Jan 10, 2011 at 14:43

3 Answers 3

3

This won't work, because it isn't actually JSON data. It is a javascript Array:

var json = [["Deutsch", "de-DE"], ["US Englisch", "en-US"]];
   // json = JSON.parse(json);
   //    ^-----Eliminate the second line.

Also, you shouldn't use click for the method name. It will interfere in some browsers.

Working Example: http://jsfiddle.net/hdX84/

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

4 Comments

Thanks for the working example! It worked on jsfiddle, but when I copied the code verbatim, it didn't work. Are there any other issues at play here? I'm using chrome to test this.
@Joshua: Did you remember to change the name of the method in your inline onclick attribute? In the example I changed it from click to click_me since using click will fail in some browsers.
I replaced click with $(document).ready(...) for the option-adding-trigger.
@Joshua: I'm not sure what you mean. I still wouldn't use click as an identifier. Without seeing your actual code, I won't be able to tell what's wrong. I'd suggest you first test it with the inline attribute. By the way, if you placed the click function inside $(document).ready(), your inline attribute won't be able to see it. It needs to be global.
2

i'd do

var mySel = $('#select')[0];
for(var i=0; i<json.length; i++){
        mySel.append(whatever);
    }

for more efficiency, because $('select') would always search through the DOM. Doesn't really matter for 10 entries, but for large pages I'd try do efficient from the start.

2 Comments

you can't call append() on a DOM object, that's a jQuery method. Doing the get [0] of the jQuery selector returns the DOM element
This also really should be posted as a comment since it doesn't deal with the issue at hand. I understand that you don't have enough rep to comment yet, but you still shouldn't abuse the Answers section. Since you only need 50 rep to comment, it shouldn't take long.
1

This is wrong:

var json = [["Deutsch", "de-DE"], ["US Englisch", "en-US"]];
json = JSON.parse(json);

You don't have JSON. You have an array of arrays. It would only be JSON if it was enclosed in single quotes (i.e. if it was a string).

var data = [["Deutsch", "de-DE"], ["US Englisch", "en-US"]];
for(var i=0; i<data.length; i++){
    $("#select").append($('<option />',{value: json[i][1], text: json[i][0]}));
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.