madcrazydrumma
madcrazydrumma

Reputation: 1897

Check if value in input equals a certain value JQuery

var enter = $('#enter').attr('value');
$('#enterButton').on("click", function() {
    if(enter == "Enter" || enter == "enter") {
        $('.loader').fadeOut(2000);
    }
});

This is my code so far, I also tried using:

var enter = $('#enter').val();

But still nothing worked, so can anyone help me fix this? Thanks :)

EDIT: the html is:

<input id="enter" type="text" placeholder="Do what it says...">
<button class="btn" id="enterButton">Go!</button>

EDIT FINAL:

The final answer is:

$('#enterButton').on("click", function() {
    var enter = $('#enter').val();
    if(enter == "Enter" || enter == "enter") {
        $('.loader').fadeOut(2000);
    }
});

Because, the variable was outside, and was not updating with the button click :) Thanks @dystroy

Upvotes: 2

Views: 15195

Answers (3)

user2542724
user2542724

Reputation: 27

You can use this as well.

$('#enterButton').on("click", function() {
var enter = $('#enter').val();
    enter = enter.toLowerCase();
    if(enter == "enter") {
        $('.loader').fadeOut(2000);
    }
});

Upvotes: 1

Denys S&#233;guret
Denys S&#233;guret

Reputation: 382102

You don't update the value of enter when you click so you only test the initial value which, of course, doesn't change.

Use this :

$('#enterButton').on("click", function() {
    var enter = $('#enter').val();
    if(enter == "Enter" || enter == "enter") {
        $('.loader').fadeOut(2000);
    }
});

Upvotes: 7

Lucas Willems
Lucas Willems

Reputation: 7063

Try this code :

var enter = $('#enter').val();
$(document).on("click", "#enterButton", function() {
    if(enter == "Enter" || enter == "enter") {
        $('.loader').fadeOut(2000);
    }
});

Upvotes: 0

Related Questions