sysmnt
sysmnt

Reputation: 11

General Dynamic Selectbox Question

This is just a general question...I'm in the process of trying to figure out how to fire an event (call a function) to populate a select (dropdown) with options once the user clicks on it. The core question is: How can I fire an event only when the select (or similar) is clicked...without firing the event again on change (on select...when the user chooses something)?

Upvotes: 1

Views: 430

Answers (1)

Luca Filosofi
Luca Filosofi

Reputation: 31173

$(function() {
    var options = ["the", "brown", "fox", "jump"];
    $('#create-select').click(function(e) {
        e.preventDefault();
        var select = '<select id="select-create">\n';
        $.each(options,function(i, item) {
            select += '<option ' + (i === 0 ? 'selected': '') + ' value="' + item + '">' + item + '</option>\n';
        });
        select +=  '</select>'; 
        $(this).fadeOut(200,function (){
         $(select).insertAfter(this);    
           $(this).remove();     
        });
    });
    $('#select-create').live('change',function() {
        alert($(this).val())
    });
});

Upvotes: 1

Related Questions