T.Steur
T.Steur

Reputation: 7

check button value jquery

I'm trying to make a certain text appear when clicking on an element when the button has a certain text. however the same text of the first if statement keeps appearing.

this is the button:

<button class="game-button">Start spel</button>

These are the table elements:

<div id="speelveld">
            <table border="0">
                <tr>
                    <td><img alt="" title="" src="img/empty.jpg" /></td>
                    <td><img alt="" title="" src="img/empty.jpg" /></td>
                    <td><img alt="" title="" src="img/empty.jpg" /></td>
                </tr>
                <tr>
                    <td><img alt="" title="" src="img/empty.jpg" /></td>
                    <td><img alt="" title="" src="img/empty.jpg" /></td>
                    <td><img alt="" title="" src="img/empty.jpg" /></td>
                </tr>
                <tr>
                    <td><img alt="" title="" src="img/empty.jpg" /></td>
                    <td><img alt="" title="" src="img/empty.jpg" /></td>
                    <td><img alt="" title="" src="img/empty.jpg" /></td>
                </tr>
            </table>
        </div>

and this is the javascript code:

$(".game-button").click(function(){
    $(".game-button").html("Reset spel");
});

if ($(".game-button").val() == "Start spel") {
    $("#speelveld tr td").click(function() {
       alert("Je kunt nog niet beginnen");
    });
} else {
    $("#speelveld tr td").click(function() {
       alert("hoi");
    });
}

my question is: how can I make a certain alert appear when the button has a certain text

Upvotes: 0

Views: 50

Answers (2)

james_bond
james_bond

Reputation: 6908

You're setting the handler on the td based on a condition which is never met.

Modify your handler to check for the value of the button every time one cell of the table is clicked.

$(".game-button").click(function(){
    $(".game-button").text("Reset spel");
});

$("#speelveld tr td").click(function() {
    if ($(".game-button").text() === "Start spel") {    
       alert("Je kunt nog niet beginnen");
    }
    else {
        alert("hoi");
    }
});

Upvotes: 1

Sandeep Nayak
Sandeep Nayak

Reputation: 4757

You are binding event inside the if. So only the event inside if gets registered

This is what you need to do:

    $("#speelveld tr td").click(function() {
       if ($(".game-button").html() == "Start spel") {
         alert("Je kunt nog niet beginnen");
       } else {            
         alert("hoi");            
       }

    });

Upvotes: 0

Related Questions