0

I have the following jquery function

$("#photographers_password_confirm").focusout(function() { 
alert('bla') 
if( $("#photographers_password") != $("photographers_password_confirm") ){
                    $("#photographers_password_error").slideDown();
            }else{
                    $("#photographers_password_error").hide();
            }   
});

it works fine in the firebug console but when I put it in the page it fails to work but it also doesnt return any error in the firebug console. what am I doing wrong?

Ahoura Ghotbi
  • 2,866
  • 12
  • 36
  • 65
  • Can you put your code along with some relevant markup on [JsFiddle](http://jsfiddle.net)? It is difficult to figure out from just the above code. – abhinav Nov 14 '11 at 09:26
  • Are you wrapping it in `$(document).ready()`? If you're adding `#photographers_password_confirm`, you need to use `$("#photographers_password_confirm").live('focusout', function () {})` instead. – Matt Nov 14 '11 at 09:29
  • 1
    you want to compare the fields values, not jQuery objects: $("#photographers_password").val() $("photographers_password_confirm").val() – Irishka Nov 14 '11 at 09:29

3 Answers3

1

in the page, are you running it "inline", as part of the HTML download, or are you running it after the document has completed its download?

my guess is that the code is running alright, but it's running before the HTML is ready to be manipulated.

make sure it's in a $(function() { /* place code here */ }); block

Kae Verens
  • 4,076
  • 3
  • 21
  • 41
0

try this:

$("#photographers_password_error").slideUp();
$("#photographers_password_confirm").focusout(function() {
    alert('bla');
    if ($("#photographers_password").val() != $("#photographers_password_confirm").val()) {
        $("#photographers_password_error").slideDown();
    } else {
        $("#photographers_password_error").slideUp();
    }
});
thecodeparadox
  • 86,271
  • 21
  • 138
  • 164
-2

If this is the exact code, then please put a ; after the alert. Some browsers take it and some don't so it would be nice to follow the same old coding style :)

Matt
  • 74,352
  • 26
  • 153
  • 180
Rohan
  • 1,705
  • 7
  • 17
  • 38
  • 1
    `Some browsers take it and some don't`... any examples? This is ridiculous AFAIK. – Matt Nov 14 '11 at 09:30
  • Here it is: http://stackoverflow.com/questions/8091606/script-onload-in-ie/8091646#8091646 – Rohan Nov 14 '11 at 09:32
  • 1
    Urr, that's a link to one of your own answers... with no evidence that `Some browsers take it and some don't`. Semi-colons is optional in JavaScript, and it's recommended you put them in yourself; but because things like `return \n 3 +4` will return undefined, not 7... not because the result is inconsistent across browsers. – Matt Nov 14 '11 at 09:35
  • Some more people think this way: http://www.webmasterworld.com/forum91/521.htm and http://stackoverflow.com/questions/8119532/does-a-semi-colon-matter-in-javascript/8119580#8119580 .. I just asked that question to be sure that it was not only me who had that issue.. And by the way, that answer of mine worked for him. Strange, but true :) – Rohan Nov 14 '11 at 09:39
  • 1
    Rohan: If you take out the semi-colon you added to *fix* the code, I bet it'd still work. If not, please post a code example. – Matt Nov 14 '11 at 09:46
  • Matt: Here is a jsfiddle someone created as an answer to my question. http://jsfiddle.net/zZcUx/1/ . By the way, I had the same argument with one of my friends over this. :) – Rohan Nov 14 '11 at 09:48
  • 1
    Rohan: What do you mean **that answer of mine worked for him**? That code snippet doesn't apply here. The *problem* in that JS Fiddle is that the `return` and the `{}` is on separate lines... that doesn't apply here. – Matt Nov 14 '11 at 09:53
  • Kindly go through "JavaScript: The Good Parts" Appendix A.3. The author clearly mentions what I am saying. In the 2nd link of my previous comment, someone mentioned about the strict behavior of some broswers while interpreting JavaScript. – Rohan Nov 14 '11 at 10:04
  • 1
    I've read the book, but I don't remember **anyone** saying `Some browsers take it and some don't` – Matt Nov 14 '11 at 10:07
  • My question to you: Who adds the missing semi-colons, in case they are not present? :) Most authors never take browsers into consideration. They take the language standards. – Rohan Nov 14 '11 at 10:09
  • Do all browsers have the same interpreter? If no, then isn't it a browser issue in the end? :) – Rohan Nov 14 '11 at 10:12
  • 1
    @Rohan: Of course they don't, but they all adhere to [the standard](http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf) (AFAIK; which was what I was asking), which describes how semi-colon insertion should occur. As they **all** apply the standard, your statement (and whole answer) `Some browsers take it and some don't` is rubbish. – Matt Nov 14 '11 at 10:19
  • 1
    @Dori: That's the short way of saying it ;) – Matt Nov 14 '11 at 10:19