0

I put in the alerts because the changes were not showing when I did inspect element. However, when I click on the object the first time it brings up the "data is now false" alert as if I had already clicked it once.

HTML:

<li class="media-thumb" data-select="false"><img src="IMG HERE"></li>

Javascript:

 $(document).ready(function() {


    $(".media-thumb").click(function() {
        if($(this).data("select") === "false") 
            {
                alert("data is now true")
                $(this).data("select", "true");


            }
        else
            {
                alert("data is now false")
                $(this).data("select", "false");

            }
    });

});
2
  • why use === and not == ? Commented Sep 20, 2013 at 3:12
  • in the selector $(".media-thumb") do you mean $(".image-thumb") Commented Sep 20, 2013 at 3:13

1 Answer 1

3

The data-select attribute is being returned as boolean false (per the jQuery docs: "Every attempt is made to convert the string to a JavaScript value"), not a string.

So you can actually write:

$(".media-thumb").click(function() {
    if(! $(this).data("select")) 
        {
            alert("data is now true")
            $(this).data("select", true);
        }
    else
        {
            alert("data is now false")
            $(this).data("select", false);
        }
});

Example CodePen: http://codepen.io/paulroub/pen/Bnvub

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

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.