0

I need to create option tags inside a select tag using JQuery,

this is the select tag:

<select id="tickets-room" style="height: 20px; width:200px; display: inline-block; margin-top:30px;" onchange="refreshRooms(this.value);">
</select>

Each option tag need an ID like 1,2,3,4

Each option tag should recieve "resp[].key" as it text.

I don't know if it makes sense, but i don't know how to explain it very well, this is the code i have with some of the elements i need. resp.key is the name i need as a text inside option tags, resp.value is an url i need to place on a button outside the select tag.

function refreshRooms(city){
    $.ajax('/services/tickets/<%= movieId %>?city=' + city).done(
        function(resp){
            alert(resp.length);
            console.log(resp[0].key + " " + resp[0].value); 
        }
    );      
}
1

3 Answers 3

2

Use jQuery.append():

function refreshRooms(city){
    $.ajax('/services/tickets/<%= movieId %>?city=' + city).done(
        function(resp){
            var sel = $("#tickets-room");

            for (var i = 0; i < resp.length; i++) {
                sel.append('<option value="' + resp[i].key + '">' + resp[i].value + '</option>');
            }
        }
    );      
}
Sign up to request clarification or add additional context in comments.

2 Comments

This approch will add only the first element of the ajax request. Must interate
@Roger I'll really appreciate if you take off your downvote now. :-)
1
function refreshRooms(city){
    var jqTickers = $('#tickets-room');
    $.ajax('/services/tickets/<%= movieId %>?city=' + city)
        .done(function(resp){
            $.each(resp, function() {
                jqTickers.append($('<option value="' + this.key + '">' + this.value + '</option>')
            });
        });
    });      
}

Comments

1

You can make new elements using the following syntax:

$('<option></option>');

This element however resides nowhere. It hasn't been added to the DOM, so it isn't visible. You'll need to use the append method or the appendTo method for that. It would look something like:

$('#tickets-room').append( $('<option></option>').attr( {'id':i,'value':resp[i].key} ).html( resp[i].key ) );

5 Comments

Thanks man, i used it inside a for event and works pretty good, only issue is, i need all the options to clear when i call refreshRoom again... How would i do it?
$('#tickets-room').html( '' ); should do the trick (you simply overwrite the html inside the tag with... well... nothing)
thanks man, i did it using ().remove();... now could u help me with one last thing? i have a button that needs to go to a specifc url given by resp[i].value... is it possible?
If you understand the code above, you should have no trouble making it ;-)
i put the url in each option's value element, i just don't know how to make the button goes to the selected options url... =/

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.