event focusout does not work with dynamic input in ASP

1

I am drawing an input dynamically in a table with

document.getElementById('agregaDevolucion').innerHTML = 
    "Devolución:<input type='text' name='txtDeposito' id='txtDeposito'/>";

and that same input searches a BD through AJAX with the event focusout of the input, with the following code

$('#txtDeposito').focusout(function(){
    this.value = this.value.toUpperCase();
    var solicitud = $('#idce').val();
    var devolucion = $('#txtDeposito').val();
    alert(devolucion);
    $.post("buscaSolicitud.asp", { solicitud : solicitud, devolucion : devolucion},
    function (data, textStatus, jqXHR) {
        console.log(data);
        var cadena = data.toString();
        console.log(data);
        if(cadena=="OK"){
            $('#btnEnvia').show();
            $('#error').text("Correcto").css({"color":"green"});;
        }else{
            $('#btnEnvia').hide();
            $('#error').text("El número de devolución no coincide con la solicitud").css({"color":"red"});
        }
    });
});

But the problem I have is that the input I draw does not throw the event focusout since when testing my application it does nothing, before making dynamic the input was static and if it worked, I do not understand why it does not do anything

    
asked by Ivxn 26.04.2016 в 20:23
source

4 answers

2

You can use addEventListener , so you do not depend on jquery . Look at JSBIN as I would solve the problem: link

The code was like this:

// Agrega el objeto dinamico
document.getElementById('agregaDevolucion').innerHTML = 
    "Devolución:<input type='text' name='txtDeposito' id='txtDeposito'/>";

// Hago referencia al objeto dinámico
var txtDeposito = document.getElementById('txtDeposito');

// Agrego el listener focusout
txtDeposito.addEventListener('focusout', function() {
  console.log("entro");
});

You should also analyze that there is a onblur event that is very similar to focusout , the code to add the event remains the same, only change one parameter when calling < strong> addEventListener .

// Agrego el listener blur
txtDeposito.addEventListener('blur', function() {
  console.log("entro");
});
    
answered by 29.04.2016 / 18:27
source
2

You can use the on() method of jQuery that allows you to associate events to an element and that works from jQuery 1.7. But you do not want to do it in the following way:

$("selector").on("evento", function() { ... });

But do it in a delegated way , that is: the event is not directly associated with an element, but it is delegated to an ancestor will be associated to the element when it is created. The format for delegated events is a bit different:

$("ancestro").on("evento", "selector", function() { ... });

When you do that, you are associating the event in a delegated manner. What you are doing is that when the element specified by "selector" is created within the element specified in "ancestor" the function will be assigned to the given event. In your particular case, you could do it like this:

$("body").on("focusout", "#txtDeposito", function() {

    this.value = this.value.toUpperCase();
    var solicitud = $('#idce').val();
    var devolucion = $('#txtDeposito').val();
    alert(devolucion);
    $.post("buscaSolicitud.asp", { solicitud : solicitud, devolucion : devolucion},
    function (data, textStatus, jqXHR) {
        console.log(data);
        var cadena = data.toString();
        console.log(data);
        if(cadena=="OK"){
            $('#btnEnvia').show();
            $('#error').text("Correcto").css({"color":"green"});;
        }else{
            $('#btnEnvia').hide();
            $('#error').text("El número de devolución no coincide con la solicitud").css({"color":"red"});
        }
    });

});
    
answered by 28.04.2016 в 18:40
1

Use% jquery .on() to dynamically attach the event

$('#txtDeposito')on("focusout", function(){ ....

jquery on ()

To dynamically create the input, use

$('#agregaDevolucion').html("Devolución:<input type='text' name='txtDeposito' id='txtDeposito'/>");

jquery html ()

    
answered by 26.04.2016 в 20:55
1

Create the input dynamically and create the function that will execute the onfocusout event, and when you create the input you add it immediately, like this:

document.getElementById("agregaDevolucion").innerHTML = "Devolución:<input type='text' name='txtDeposito' onfocusout='myFunction(this);' id='txtDeposito'/>";

Thus, when creating the input, it will be immediately linked to the event onfocusout , and your function should look like this:

function myFunction(input) {
        input.value = input.value.toUpperCase();
        var solicitud = $('#idce').val();
        //alert(solicitud);
        var devolucion = $('#txtDeposito').val();
        alert(devolucion);
        //....................
}

I tried it and if I launched the event.

    
answered by 28.04.2016 в 15:17