-1

I load a peace of html code with ajax, and I need to set a mask on a input element within this loaded html.

I load the html:

    // procurar clientes
$("#submit-busca").click(function (){

    nome = $("#nome-busca").val();

    if(nome == ''){
        alert("Busca não especificada.");
        return false;
        }

    // get tabela de resultados
    $.post( "php/processa-busca-cliente-atendimento.php", { id: <?php echo $user_id; ?>, nome: nome })
    .done(function( data ) {
    $("#resultado-busca").html(data);
    });

    return false;

    });

And then try to set the mask:

$( document ).on( "load", ".cell-cpf", function() {
        $('.cpf').mask('999.999.999-99');
        });

The code above, only work if I use "click", but I want to set the mask on "load" or "ready". I try this but dont work. Do i speak clear?

Hedron Dantas
  • 616
  • 2
  • 6
  • 16
  • The "solution" that I found was use 'mouseover', like this `$( document ).on( "mouseover", ".cell-cpf", function() { $('.cpf').mask('999.999.999-99');});` but its not the right do to. – Hedron Dantas Mar 19 '14 at 05:05

1 Answers1

2

Try this,

$( document ).ready( function() {
    $('.cpf').mask('999.999.999-99');
});

Also in $.post,

$.post( "php/processa-busca-cliente-atendimento.php", { id: <?php echo $user_id; ?>, nome: nome })
.done(function( data ) {
   $("#resultado-busca").html(data);
   $('.cpf').mask('999.999.999-99');
});

Updated, then try DOMSubtreeModified like,

$(function(){
    $(document).bind('DOMSubtreeModified', function () {
       if ($('.cpf').length) {
           $('.cpf').mask('999.999.999-99');
       }
    });
});

Read jquery-does-not-execute-on-document-change

Community
  • 1
  • 1
Rohan Kumar
  • 40,431
  • 11
  • 76
  • 106
  • Doesn't work, because the append document dos not make part of DOM, even after `$("#resultado-busca").html(data);`, the class `.cpf` will not be founded by `$('.cpf').mask('999.999.999-99');` because it not exist in DOM. – Hedron Dantas Mar 19 '14 at 05:02
  • To add more masks I have to add one more if estatment like `if($('.other').length) {$('.other').mask('999.999.999-99');}` – Hedron Dantas Mar 19 '14 at 07:46