Element Higher than Input

0

Good morning, I'm putting a mask on a field that formats a phone number, but you can see that the field ID is # Field_11690 because it was created by a WordPress plugin.

( function( $ ) {

// jQuery MeioMask.
$( '#field_11690' ).setMask( '(99) 9999-99999' ).ready( function( event ) {
    var target, phone, element;
    target = ( event.currentTarget ) ? event.currentTarget : event.srcElement;
    if ( 'undefined' === typeof type ) {
        return;
    }
    phone = target.value.replace( /\D/g, '' );
    element = $( target );
    element.unsetMask();
    if ( 10 < phone.length ) {
        element.setMask( '(99) 99999-9999' );
    } else {
        element.setMask( '(99) 9999-99999' );
    }
});

And for this reason I do not know if production would continue the same ID, is there any other way for me to be able to declare the ID number of this field, or to pick up some Upper element?

    
asked by anonymous 27.11.2018 / 14:00

2 answers

0

If you are not sure how to do this,

$('.field_telefone-com-ddd').find(':input').setMask('(99) 9999-99999').ready(function( event ) {
    var target, phone, element;
    target = ( event.currentTarget ) ? event.currentTarget : event.srcElement;
    if ( 'undefined' === typeof type ) {
        return;
    }
    phone = target.value.replace( /\D/g, '' );
    element = $( target );
    element.unsetMask();
    if ( 10 < phone.length ) {
        element.setMask( '(99) 99999-9999' );
    } else {
        element.setMask( '(99) 9999-99999' );
    }
});

This code will capture all elements ( find () of type input within the element with the reference class within $ , so it will only work in your situation, where there is only one input.

If there were more than one input, you could also capture the X-order input with:

$('.field_telefone-com-ddd').find(':input:eq(X)')

Substituting 'X' for the order number of the input within the reference element.

    
27.11.2018 / 15:57
0

Deepening Vinicius' Answer above (which has already assured me the correct answer) I also looked for another way to solve it and I got the code below.

( function( $ ) {
// jQuery MeioMask.
if ($('.field_telefone-com-ddd input').length !== 1) {
    return;
}
$('.field_telefone-com-ddd input').setMask('(99) 9999-99999').ready(function( event ) {
    var target, phone, element;
    target = ( event.currentTarget ) ? event.currentTarget : event.srcElement;
    if ( 'undefined' === typeof type ) {
        return;
    }
    phone = target.value.replace( /\D/g, '' );
    element = $( target );
    element.unsetMask();
    if ( 10 < phone.length ) {
        element.setMask( '(99) 99999-9999' );
    } else {
        element.setMask( '(99) 9999-99999' );
    }
});

} (jQuery));

    
27.11.2018 / 19:26