0

i ma using this code to disable ValidUntil field when ExpirationTime has a valued, the problem this only work when i made a change but not when i edit the record, is there any way to modify this code for always disable Validuntil if Expirationtime has a value?

$('#x_ExpirationTime').on('change', function() {
if(this.value.length > 0)
$('#x_ValidUntil').attr('disabled','disabled');
else
$('#x_ValidUntil').removeAttr('disabled');
}); 

Thanks in advance

2
  • What do you mean by "edit the record"? Commented Mar 19, 2019 at 16:55
  • 1
    Can you kindly share more code. The HTML if you can and let us know where you are setting the above function. Thanks. Commented Mar 19, 2019 at 16:55

1 Answer 1

1

You need to execute the code inside the event without the change action to make it work by itself. You have a few options:

  1. Force the event execution:

    $('#x_ExpirationTime').on('change', function() { }).change();
    

    Add change() at end of the event binding to execute it after the page initialization. It will work as if user has changed it;

  2. Separate the event body in a function:

    function checkValue() {
        let $input = $('#x_ValidUntil');
        if($('#x_ExpirationTime').val().length > 0)    
            $input.attr('disabled','disabled');
        else
            $input.removeAttr('disabled');
    }
    
    $('#x_ExpirationTime').on('change', checkValue);
    
    checkValue(); 
    

    The isolated call to checkValue() will make the magic.

Both ways will have the same outcome.

Sign up to request clarification or add additional context in comments.

3 Comments

Hello, first thanks, this code is working but in wrong way, is disable when ExpirationTime is null, i need disable when ExpirationTime is not null, how fix this thanks
Thanks now work you rock, thanks for all help everyvary
@Ninja you probably have inverted the condition, right? Np, you're welcome!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.