1

I currently try to get into JQuery and, to be more specific, into AJAX requests.

Code:
Javascript:

success: function(json) {
        console.log(json);
        var $el = $("#system");
        $el.empty(); // remove old options
        $el.append($("<option></option>")
            .attr("value", '').text('Please Select'));

        // for each set of data, add a new option
        $.each(json, function(value, key) {
            $el.append($("<option></option>")
                .attr("value", value).text(key));
        });

},

HTML:

<form id="filter">
    <label for="datepicker">
        Date: <input id="datepicker" onchange="updateSystems()" type="text" />
    </label>
    &nbsp;
    <label>from:
        <select id="timespan-from"  name="timespan-from" onchange="updateSystems()" size="1">
            <option value="0">0 Uhr</option>
            ...
            <option value="23">23 Uhr</option>
        </select>
    </label>
    &nbsp;
    <label>to:
        <select id="timespan-to" name="timespan-to" onchange="updateSystems()" size="1">
            <option value="0">23 Uhr</option>
            ...
            <option value="0">0 Uhr</option>
        </select>
    </label>
    &nbsp;
    <label>in System:
        <select id="system" name="system" size="1">
            <!-- this should be updated -->
            <option>Amarr</option>
            <option>JEIV-E</option>
            <option>Jita</option>
            <option>Litom</option>
            <option>Penis</option>
        </select>
    </label>
</form>

This function is triggered whenever I change a field in my document, and should update the elements of a html dropdown.

The AJAX request works, but the dropdown-update does not work as intended. I try to add some <option> fields to the <select> dropdown with the id id=system.

However, the result dropdown always looks like this:

  • Please Select
  • [object Object]

What needs to be changed so my functions adds my json data to my dropdown menu?

Example JSON return from my php script:

[
    {
        "solarSystemID": "30001171",
        "solarSystemName": "F4R2-Q"
    },
    {
        "solarSystemID": "30001182",
        "solarSystemName": "MB-NKE"
    },
    {
        "solarSystemID": "30004299",
        "solarSystemName": "Sakht"
    },
    {
        "solarSystemID": "30004738",
        "solarSystemName": "1-SMEB"
    }
]

solarSystemID should become the value of the <option>,
solarSystemName should be the text.

Thanks in advance for any help, I guess I just need a little push in the right direction for it to finally work as intended.

4
  • When you debug, what are the values of key and value that you're using to build the option element(s)? It would appear that one of them is an object, not a literal value. Commented Sep 8, 2016 at 12:12
  • 3
    Please show all relevant code in the question itself. We shouldn't have to go off site to review your issue. Questions should be self contained Commented Sep 8, 2016 at 12:13
  • value would be 0..1..2..3 ... key would be an Object every time ... coerced to a string an object becomes [object Object] - by the way, jQuery.each callback arguments are key, value, not value, key - you're possibly thinking of Array#forEach Commented Sep 8, 2016 at 12:13
  • @charlietfl I will try to update my anwser so the JS code is actually in the post. You are totally right, the github-content will change and then the question becomes unclear. Will fix it now. Commented Sep 8, 2016 at 12:23

1 Answer 1

4

you can use the map method for mapping Solar object to option element.

$('#system').append(json.map(function(sObj){
    return '<option id="'+sObj.solarSystemID+'">'+ sObj.solarSystemName +'</option>'
}));
Sign up to request clarification or add additional context in comments.

3 Comments

I like this because it does a single append rather than an append for each item! which makes browsers happy :p
This works like a charm, thanks! However, there is a closing ) missing at the end!
fixed it , glad I could help :)

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.