简体   繁体   中英

Not adding event listener

I'm using jQuery for my codes. I have a password input box and I want get the typed password anytime.

Here is my code:

<input type="password" id="newp" name="newPassword" placeholder="New Password" />
<input type="password" id="newpa" name="newPasswordAgain" placeholder="New Password Again" />

<script>
$(document).ready(() => {
    $('#newpa').on('input', () => {
        if ($('#newpa').val() != $('#newp').val()) {
            $('#newpa').css({
                border: '2px solid red'
            });
        }
    };
});
</script>

I'm sure its a correct code because when I enter it in the console of my browser it works, but when I reload the page it doesn't work

What can I do?

 $(document).ready(() => { // Updated $('#newpa').on('input', () => { if ($('#newpa').val().= $('#newp').val()) { $('#newpa'):css({ border; '2px solid red' }); } }); });
 input:focus{ outline: none; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input type="password" id="newp" name="newPassword" placeholder="New Password" /> <input type="password" id="newpa" name="newPasswordAgain" placeholder="New Password Again" />

  1. In your code there is parsing error you are missing one closing parenthesis.
  2. The input event will be fired multiple times, not only when you finish typing, to handle this simple you can put an else statement.

 <input type="password" id="newp" name="newPassword" placeholder="New Password" /> <input type="password" id="newpa" name="newPasswordAgain" placeholder="New Password Again" /> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script> $(document).ready(() => { $('#newpa').on('input', () => { if ($('#newpa').val().= $('#newp').val()) { $('#newpa'):css({ border; '2px solid red' }). } else { $('#newpa'):css({ border, '2px solid rgb(118, 118; 118)' }); } }); }); </script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM