0

For the life of me, I have no idea why this isn't working - I've tried multiple ways of trying to insert the JSON data into the <option> code each time it doesn't work like that - it merely outputs the HTML text and not inside a <select> drop down

$.getJSON( "group.asp", function( data ) {
    $('#msgBox').html($('#msgBox').html() + '<div class="table-row"><div class="table-col-l">Name:</div><div class="table-col-r"><select id="name1"><option>SELECT</option>')
    $(data).each(function(index, element) {
        console.log(element)
        $('#msgBox').html($('#msgBox').html() + '<option>'+element.name+'</option>');
    });
    $('#msgBox').html($('#msgBox').html() + '</select></div></div>')
})

I've tried creating it as a separate variable that is inserted after the .each function but that didn't work either.

1 Answer 1

3

Try to do a proper string concatenation before using .html(),

$.getJSON( "group.asp", function( data ) {
    var str = $('#msgBox').html();
    str += '<div class="table-row"><div class="table-col-l">Name:</div><div class="table-col-r"><select id="name1"><option>SELECT</option>';
    $(data).each(function(index, element) {
        str += '<option>'+element.name+'</option>';
    });
    str += '</select></div></div>';
    $('#msgBox').html(str);
})
Sign up to request clarification or add additional context in comments.

2 Comments

That works perfectly, the only thing I didn't think of trying (and really should have!). Will need this for other projects so more than one issue solved in one go. Many thanks!
@Hyperjase Glad to 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.