0

I got the following code to change text in a input if it has presence of a text set in the attribute texttoreplace of this same input:

    $(".replace_path").focusout(function() {
        $(".replace_path").val($(".replace_path").val().replace($(this).attr('texttoreplace'),""));
    });

Its working fine except I can only trigger when the focus is out. Is there a way to accomplish the same result triggering the event in case value > 0? Cause I got another java script source adding the text to that input. (its not being typed).

Thanks,

2 Answers 2

1

You can use change() event listener:

$('.replace_path').change(function() {
  if ( $('.replace_path').val() > 0 ){
    alert('Handler for .change() called.');
  }
});

But you should be careful about starting an infinite recursive loop, because focusout will update the value of .replace_path and this will trigger the change callback function.

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

Comments

0

The problem with .change() and .focusout(), is that they both require the user to .blur() the event before it fires.

You can just trigger the event directly though:

function yourJavaScript() {
   $(".replace_path").val('your custom text');
   $(".replace_path").trigger('focusout');
}

Comments

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.