0

having a little issue with getting this simple if/else to work.

$(document).ready(function () {
    $(".sme").click(function (event) {

        var vote = $(this).attr('id');
        $('.to').html(vote);

        $(".sme").removeClass('clicked');
        $(this).addClass('clicked');
    });
});


$(document).ready(function () {
    $(".tme").click(function (event) {

        var more = $(this).attr('id');
        $('.more').html(more);


        if (typeof vote) {

            $('.error').html("ERRRROOORRRR");

        } else {

            $(".tme").removeClass('clicked');
            $(this).addClass('clicked');

        }

    });
});

Having an issue getting the if/else to work.

Any help would be great, Thanks!

6
  • What's your issue? What's the error? Commented May 31, 2013 at 0:55
  • Not getting an error, but it won't let me perform what I want in regards to the if/else area. Commented May 31, 2013 at 0:55
  • 1
    vote is a string, so it is not boolean :) you must compare it to something. It does not work like in PHP $vote = "1"; if ($vote)... Commented May 31, 2013 at 0:56
  • What do you want it to do? It's not clear from your post Commented May 31, 2013 at 0:56
  • what is this? typeof vote Commented May 31, 2013 at 0:56

2 Answers 2

2

vote will always be undefined because it is not scoped in your click handler function for .tme. I think you want something like this:

$(document).ready(function () {
    var vote = undefined;

    $(".sme").click(function (event) {

        vote = $(this).attr('id');
        $('.to').html(vote);

        $(".sme").removeClass('clicked');
        $(this).addClass('clicked');
    });

    $(".tme").click(function (event) {

        var more = $(this).attr('id');
        $('.more').html(more);


        if (vote === undefined) {

            $('.error').html("ERRRROOORRRR");

        } else {

            $(".tme").removeClass('clicked');
            $(this).addClass('clicked');

        }

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

1 Comment

typeof returns a string, not the value undefined.
1

vote is a local variable for the click event handler. You can't call it inside another event handler, it will not exist.

Why are you using two document ready handlers? Just use one. I would do something like this:

<script type="text/javascript">
$(document).ready(function() {
    $(".sme").click(function (event) {

        window.vote = $(this).attr('id');
        $('.to').html(window.vote);

        $(".sme").removeClass('clicked');
        $(this).addClass('clicked');
    });

    $(".tme").click(function (event) {

        var more = $(this).attr('id');
        $('.more').html(more);

        // Two cases. If it's undefined will throw error. The same if it is a string, but its length equals to zero.
        if ( typeof window.vote == undefined || window.vote.length == 0 ) {

            $('.error').html("ERRRROOORRRR");

        } else {

            $(".tme").removeClass('clicked');
            $(this).addClass('clicked');
        }
    });
});
</script>

3 Comments

What other options does he have? He could save the value inside a DOM element, but that'll be the same as using a global variable (but more expensive in terms of access times). Anyway, as the "vote" variable is defined locally in $(".sme").click(), it will not be in the scope of $(".tme").click(). I don't see other options.
It would be better practice to create a variable scoped inside $(document).ready(function() {. That would be accessible to both handlers without creating a global variable.
Yeah, I see your point. Anyway, if he's using jQuery events for everything, it will act "like" a global variable. Still, thank you for your comment, you're right.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.