0

noob question...

Had the same kind of question yesterday. Managed to get it working without an answer. Today, same script, different divs -not working.

Html of it

<div class="imgform">
<form>
Login:
<input type="text" id="imglogin" name="login">
Password:
<input type="password" id="imgpass" name="pass">

<a class="imglogin" href="#">Login</a>
</form>
</div>

And the script it self

$(".imglogin").click(function(){

    var password = "111";
    if($("#imgpass").val() !== password) {
        $("#imgform").text("Incorrect password");
    } else {
        window.location.href = "http://google.com";   

} });

Went over the script a few times. Can't figure out where is my mistake

1
  • What happens? What error do you get? Have you debugged with the console? Commented Apr 9, 2014 at 16:52

3 Answers 3

1

You must load jquery in <head> block

<script src="//code.jquery.com/jquery-1.9.1.js" type="text/javascript">

HTML (added a div with id of imgform to display any answers)

<div class="imgform">
    <form>Login:
        <input type="text" id="imglogin" name="login">Password:
        <input type="password" id="imgpass" name="pass">
<a class="imglogin" href="#">Login</a>
            <div id="imgform"></div>
    </form>
</div>

Try this

$(window).load(function(){
$(".imglogin").click(function () {
console.log('working')
    var password = "111";
    if ($("#imgpass").val() != password) {
        $("#imgform").text("Incorrect password"); //I added div with id imgform
    } else {
        window.location.href = "http://google.com";
    }
});
});

DEMO

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

5 Comments

Change #imgform to .imgform.
@JayBlanchard see demo fiddle html i added new div
that wasn't apparent here. Add those changes so that the code is preserved with your answer.
@JayBlanchard thats a perfect way to show error text thats why OP accept this as answer
I know that it is, but the code may be lost if something goes wrong with jsfiddle. Please add the changes here so that SO can preserve the code for future readers.
1

You should use .imgform instead of #imgform when password wrong and another one is missing curly brace }

Check this Demo jsFiddle

jQuery

$(".imglogin").click(function(){

    var password = "111";
    if($("#imgpass").val() != password) {
        $(".imgform").text("Incorrect password");
    } else {
        window.location.href = "http://google.com";   
    }
});

HTML

<div class="imgform">
<form>
Login:
    <input type="text" id="imglogin" name="login" />
Password:
    <input type="password" id="imgpass" name="pass" />

<a class="imglogin" href="#">Login</a>
</form>
</div>

Comments

0

You're missing a closing curly brace for your if statement -

$(".imglogin").click(function(){

    var password = "111";
    if($("#imgpass").val() !== password) {
        $(".imgform").text("Incorrect password"); // using a class instead of an id here
    } else {
        window.location.href = "http://google.com";   
    } // right here
});

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.