Dynamically triggering combos for DOM creation via jQuery

Asked

Viewed 70 times

1

I have a sequence of 3 dropdowns that modify the DOM. One depends on the other. If I select the first dropdown it modifies the DOM and the other dropdown. The second dropdown modifies the third in the same way. The question is how to dynamically trigger these dropdowns? I can fill in the first.

 $('select#initial option[value=' + data[0].initial + ']').attr({ selected : "selected"}).prop('disabled', false);;

But the change event that would mount the DOM of the second dropdown was not triggered.

  • I found the answer $("select#initial"). Trigger("change");

  • It would be nice to publish the solution as a response, and if you put the link to documentation, perfect :)

1 answer

1

Buddy, try it this way, I created a select and in the event change, create other elements, all dynamic via DOM.

$(document).ready(function(){
$('#cboEstado')
    .append('<option>São Paulo</option>')
    .append('<option>Rio de Janeiro</option>')
    .append('<option>Minas Gerais</option>');

$('#cboEstado').on('change',function(){

    //Create a new elemment dropdown
    $('<select>')
        .prop('id','cboCidade')
        .append('<option>Araraquara</option>')
        .append('<option>Campinas</option>')
        .insertAfter($('#cboEstado'));

    //Attach event change in the new elemment
    $('#cboCidade').on('change',function(){
        $('<select>')
            .prop('id','cboItems')
            .append('<option>Item 1</option>')
            .append('<option>Item 2</option>')
            .insertAfter($('#cboCidade'));
    });
});

});

Example code in the link below: http://jsfiddle.net/rboschini/th547cnf/

(゚ ヮ ゚)

  • This example of yours doesn’t automatically trigger an event. Or rather, it fires as long as the user takes the first action. To trigger an event automatically you need to use the JQUERY Trigger. The documentation is here http://api.jquery.com/trigger/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.