Nikki
Nikki

Reputation: 79

JavaScript Help Checking Passwords Match

I have this code that works on this site: http://jsfiddle.net/FPNBe/2/

However, the exact code doesn't work on my site. (With the exception I had to add

<script TYPE="text/javascript"> & < /script> around the javascript.)

Is there something I'm missing?

Upvotes: 2

Views: 1070

Answers (2)

PleaseStand
PleaseStand

Reputation: 32082

This line:

$('#password, #confirmpassword').keyup(function() { checkPass(); } );

Must only be executed after the elements in question have been added to the DOM (Document Object Model) by the browser, so that jQuery can find them. To wait until the DOM is ready, meaning that all the page's elements are accessible, you can put this line inside a function, which you can pass to jQuery:

$(document).ready(function() {
    $('#password, #confirmpassword').keyup(function() { checkPass(); } );
});

Or a bit shorter:

$(function() {
    $('#password, #confirmpassword').keyup(function() { checkPass(); } );
});

Upvotes: 2

Alex R.
Alex R.

Reputation: 4754

Wrap the script inside $(document).ready()

$(document).ready(function () {
  $('#password, #confirmpassword').keyup(function() { checkPass(); } );
});

Upvotes: 2

Related Questions