Compare 2 fields <input>, clear the second one

Advertisements

If the first and second fields have the same text despite the font and spaces, then delete the second one text

$(document).ready(function() {
  var input_1 = $(".test_1");
  var input_2 = $(".test_2");
  $(input_2).focusout(function() {
    if (input_1.val() === input_2.val()) {
      $('.test_2').val('');
    }
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
<input value="dom" class="test_1">
<input value=" Dom" class="test_2">

>Solution :

Perhaps you want a trim and a lowerCase?

$(function() {
  const $input_1 = $(".test_1");
  const $input_2 = $(".test_2")
  .on("blur",function() {
    const val1 = $input_1.val().trim().toLowerCase(), 
          val2 = $input_2.val().trim().toLowerCase();
    console.log(val1,val2,val1 === val2);
    if (val1 === val2) {
      $input_2.val('');
    }
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
<input value="dom" class="test_1">
<input value=" Dom" class="test_2">

Leave a ReplyCancel reply