V
V
VitaliySm2015-11-13 12:38:30
JavaScript
VitaliySm, 2015-11-13 12:38:30

Does not detect an event when a selector is selected, how to catch this event correctly?

$('#id-category').bind('change', function() {
    selected = $('#id_1-category option:selected').val();
    if (selected != ''){
        $.get('/get/?category=' + selected, function(data) {
        $('#make_container').html(data);
        });
    }
});

$('#make_container').bind('change', function() {
    console.log('----')
    }
});


There is a first selector when we select a category there, a selector for the second input is formed, the problem is that for some reason it does not catch the change of this newly created selector

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Evgeny Svirsky, 2015-11-13
@VitaliySm

The problem is obvious. You are using the wrong construct to catch the event of a dynamically created element. Here you can read:
api.jquery.com/on
You need to use Delegated events.
You need to use it like this:

$( "body" ).on( "change", "#make_container", function() {
   console.log('----')
});

Scope "body" - can be scaled down to a tracked container...

V
Victor, 2015-11-13
@master2016

make sure your bind works. If not, it's better to replace it with the .on() construct

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question