Clear input on focus

Clear input on focus

bbrindzabbrindza Posts: 300Questions: 69Answers: 1

I need to clear an inline table input field on focus and replace default value if on blur is blank.

I tried a JQuery script in my document.ready function which I have used in other web forms which works.

I can not get this to work in DataTable Editor . It looked everywhere for an alternate solution but came up empty.

Any insight is welcomed.

var default_value = '' //Global var

    $('#DTE_Field_HRWPER').focus(function(){
         default_value = this.value
        this.value = '';
    });

     $('#DTE_Field_HRWPER').blur(function(){
        if (!this.value.length) {
            this.value = default_value;
        }
    });

Replies

  • allanallan Posts: 62,003Questions: 1Answers: 10,164 Site admin

    Use the initSubmit event. There you can check if a field's value is empty and if so set it to be a different value.

    Allan

  • bbrindzabbrindza Posts: 300Questions: 69Answers: 1

    That seems like a good event, but maybe not for my intended purpose. I need a onFocus like event.

    When a user tabs to or mouse clicks into a inline field that already contains data, I need save the data in a temp variable and set the field to blank. If the user leaves the field (other then submit) without entering any new data, then set it back to the original value found in the temp variable.

    It appears the initSubmit can not achieve this.

  • allanallan Posts: 62,003Questions: 1Answers: 10,164 Site admin
    editor.field('myField').input().on( 'focus', ... );
    

    can be used. field().input() will give you the jQuery object for the input element.

    Allan

  • bbrindzabbrindza Posts: 300Questions: 69Answers: 1

    Looks like that is what I needed. Here is the working script. Thank you!

    var defaultInputValue // Global
     editor.field( 'HRWPER' ).input().on( 'focus', function () {
            defaultInputValue = editor.field( 'HRWPER' ).val() ;
            if (editor.field( 'HRWPER' ).val() === defaultInputValue ) {
    
                editor.field( 'HRWPER' ).set( '' );
            }
        } );
    
     editor.field( 'HRWPER' ).input().on( 'blur', function () {
            if (editor.field( 'HRWPER' ).val() === '' ) {
    
                editor.field( 'HRWPER' ).set(defaultInputValue);
            }
        } ); 
    
This discussion has been closed.