0
var change = 1;

$("#game").click(function (e) {
    if (change == 1) {
        var data = $(e.target).closest("td").text();
        var p1 = $("#player1").val();
        var p1_value = p1 + data;
        $("#player1").val(p1_value);
        change = 2;
    } else(change == 2) {
        var data = $(e.target).closest("td").text();
        var p2 = $("#player2").val();
        var p2_value = p2 + data;
        $("#player2").val(p1_value);
        change = 1;
    }

});

Is it the write way to write? BTW here it is not going to the else loop.

2
  • @jijq what is the problem in above code?? Commented Dec 11, 2010 at 17:07
  • 3
    jQuery isn't a language you know. JavaScript is a language. Commented Dec 11, 2010 at 17:10

2 Answers 2

1

JavaScript has only function scope, not "curly bracket scope". If you want to be very correct, you would put all the declarations at the top of the function:

$("#game").click(function(e) {
    var data = '',
        p1 = '',
        p1_value = '',
        p2 = '',
        p2_value = '';

        if (change == 1) {
            data = $(e.target).closest("td").text();
            p1 = $("#player1").val();
            p1_value = p1 + data;
            $("#player1").val(p1_value);
            change = 2;
        }
        else if (change == 2) { // you forgot `if`
            data = $(e.target).closest("td").text();
            p2 = $("#player2").val();
            p2_value = p2 + data;
            $("#player2").val(p1_value);
            change = 1;
        }
});

At least JSLint complains about data being already defined.

But you can optimize (and shorten!) your function in another way:

$("#game").click(function(e) {
    var $player = $('#player' + change);
    $player.val($player.val() + $(e.target).closest("td").text());
    change = (change === 1) ? 2 : 1;
}
Sign up to request clarification or add additional context in comments.

10 Comments

+1 I was trying to leave an example like your last one, but did two really stupid things. Stupid 1: Accidentally created infinite loop. Stupid 2: Used alert(). You'd think I'd learn by now!
@patrick dw: Poor you... I know how annoying that can be ;)
@Felix: In case you're interested, looks like when setting up the toggle event, jQuery triggers the handler, and if your javascript throws any kind of error, the handler continuously fires (in Safari/Chrome anyway). $("div").toggle(function(e){throw('something');}); If there's a <div> on the page, there's an automatic infinite loop. Off to the bug tracker...
@patrick dw: Actually it is, there are two versions of this method: api.jquery.com/toggle and api.jquery.com/toggle-event. The first one only accepts one callback. And now that I see it, if you use only one function, you are not binding the function as click event handler... it is just the callback that is called after the element is hidden (or shown) (this "method overloading" can be really confusing in jQuery). It is still a bug imo.
@Felix: Yep, I just realized that about a minute after filing. I should have checked back here first. I had actually filed it as an enhancement to give consideration to a single function passed as a typical click(). Then it hit me, so I updated it with a comment. In my opinion, it is a very unfortunate decision to have the same method perform entirely different tasks.
|
1

Your else should be an else if if you intend to use the condition:

if (change == 1) {
    // ...
} else if(change == 2) {
    // ...
}

As perhaps a better alternative, you can use jQuery's .toggle(), and pass it two functions which alternate on clicks.

$("#game").toggle(function(e) {
            var data = $(e.target).closest("td").text();
            var p1 = $("#player1").val();
            var p1_value = p1 + data;
            $("#player1").val(p1_value);
 }, function(e) {
            var data = $(e.target).closest("td").text();
            var p2 = $("#player2").val();
            var p2_value = p2 + data;
            $("#player2").val(p1_value);
 });

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.