Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery on() event not firing for sweetalert2 textboxes

I have dynamically created textboxes in sweetalert2 like this:

swal({
    title: 'Enter Info',
    showCancelButton: true,
    html:   "<table>" +
                "<tr>" +
                    "<td>name</td>" +
                    "<td><input type='text' id='name'/></td>" +
                "</tr>"
                "<tr>" +
                    "<td>email</td>" +
                    "<td><input type='text' id='email'/></td>" +
                "</tr>"
            "</table>"
}).then(function(){
    // ajax
});

And jQuery function to listen textbox change event.

$(document).ready(function () { 
    <script type="text/javascript">
        $('#name').on('change', function(e) {
            console.log($(this).val());
        });
    </script>
});

But event is not being fired when changing textbox values inside sweetalert2. jQuery is properly loaded and it works on other textboxes outside of sweetalert2 model. I've also tried adding <script>...</script> after </table> in above html: but still no luck. Can somebody help me out please? Any input would be greatly appreciated.

like image 920
Min Naing Oo Avatar asked Feb 06 '23 11:02

Min Naing Oo


2 Answers

change $('#name').on('change', function(e) { to $(document).on('change','#name', function(e) {

  1. Delegate the event properly
like image 53
guradio Avatar answered Feb 09 '23 01:02

guradio


this happens because you are using

$('#name').on('change', function(e) {});  // this works for static dom

$(document).on('change','#name', function(e) {});  // this works for static as well as content dynamically added in dom.
like image 35
Mayank Pandeyz Avatar answered Feb 09 '23 00:02

Mayank Pandeyz