2

I have a simple JSON structure but I can't get the append right to the page. All the names of each teams are in one option tag. They should be in separate option tags.

I have searched and read somewhat similar questions but they are never similar enough that I could understand them.

Once I did manage to get them appended separately but then there were duplicates and I didn't know how to fix the duplication problem.

PS: I presume that my append structure is not the best practice. I am quite new to JavaScript and don't know a better way yet. if you have better solutions they are welcome.

The correct HTML structure should be:

<fieldset class="team-wrap">
    <label for="Team A"><img src='images/player1.png' alt="Team A">
        <select name="Team A" id="Team A" multiple class="team dropdown selectpicker show-menu-arrow show-tick form-control" title="Players" data-width="70%" data-size="auto" multiple data-selected-text-format="count">
            <optgroup label="Select a Player(s)">
                <option value="Merv Jake">Merv Jake</option>
                <option value="Derek Dax">Derek Dax</option>
                <option value="Trace Harper">Trace Harper</option>
            </optgroup>
        </select>
    </label>
</fieldset>

JSON:

{
    "men": [{
        "Team A": {
            "img": "images/player1.png",
            "names": ["Merv Jake", "Derek Dax", "Trace Harper"],
            "group": "A"
        },
        "Team B": {
            "img": "images/player2.png",
            "names": ["Shannon Xavier", "Alec Xavier", "Simon Leslie"],
            "group": "B"
        }
    }]
}

jQuery:

function teams(gender){
    var teams = $(".teams").find(".group");
    $.getJSON('inc/miehet.json', function(data) {
        $.each(data[gender], function(key, value) {
            $.each(value, function(key) {
                //console.log(key + " = " + this.img + " , " + this.names + " , " + this.group);
                teams.append(
                    '<fieldset class="team-wrap">'+
                        '<label for='+key+'><img src='+this.img+' alt='+key+'>' +
                            '<select name='+key+' id='+key+' multiple class="team dropdown selectpicker show-menu-arrow show-tick form-control" title="Players" data-width="70%" data-size="auto" multiple data-selected-text-format="count">' +
                                '<optgroup label="Select a Player(s)">' +
                                    '<option value="'+this.names+'">'+this.names+'</option>' +
                                '</optgroup>' +
                            '</select>' +
                        '</label>' +
                    '</fieldset>'
                );
            });
        });
    });
}
teams("men");
2
  • 1
    You need $.each(this.names, ...) to append a separate <option> for each name. Commented May 7, 2016 at 22:39
  • @barmar I did try that but then i did get duplicates. i think that i didn't apply it correctly. How would i do that? Commented May 7, 2016 at 22:44

1 Answer 1

1

This worked for me based off of @Barmar 's comment:

http://codepen.io/stufu/pen/QNYdXV

function teams(gender){
    var teams = $(".teams").find(".group");
    $.getJSON('https://demo8858242.mockable.io/test', function(data) {
        $.each(data[gender], function(key, value) {
            $.each(value, function(key) {
                teams.append(
                    '<fieldset class="team-wrap">'+
                        '<label for='+key+'><img src='+this.img+' alt='+key+'>' +
                            '<select name='+key+' id='+key+' multiple class="team dropdown selectpicker show-menu-arrow show-tick form-control" title="Players" data-width="70%" data-size="auto" multiple data-selected-text-format="count">' +
                                '<optgroup class="' + this.group + '" label="Select a Player(s)">' + '</optgroup>' +
                            '</select>' +
                        '</label>' +
                    '</fieldset>'
                );
                var group = $('.' + this.group);
                $.each(this.names, function(key, name) {   
                  group.append('<option value="'+name+'">'+name+'</option>');
                });
            });
        });
    });
}
teams("men");
Sign up to request clarification or add additional context in comments.

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.