-1

This works (It prints some text to the screen):

$(document).ready(function() {

var uspTot = 1
var keyTot = 5

    $('.field .button').click(function(){
    var theSibling = $(this).siblings('div').attr('id');
    if (theSibling == 'usp'){
       $(this).before('the string is usp')
       uspTot++ 
    } else if (theSibling == 'keywords')(
       $(this).before('the string is keywords') 
    )
});

However this doesn't do anything:

$(document).ready(function() {

var uspTot = 1
var keyTot = 5

    $('.field .button').click(function(){
    var theSibling = $(this).siblings('div').attr('id');
    if (theSibling == 'usp'){
       $(this).before('the string is usp')
       uspTot++ 
    } else if (theSibling == 'keywords')(
       $(this).before('the string is keywords')
       keyTot++
    )
});

The only difference between the two is this bit of code:

keyTot++

I can't understand why this is completely breaking my basic script so I'm hoping someone here can point and say "Oh that's because you've forgotten to XXXX, you daft thing" - Or words to that effect.

gdoron
  • 147,333
  • 58
  • 291
  • 367
Username_null
  • 1,249
  • 2
  • 21
  • 29
  • 1
    What if you use semicolons (as IMO you should generally do in JS)? – Dave Newton Nov 13 '12 at 16:32
  • @DaveNewton That's completely stylistic and nothing to do with the problem. – James M Nov 13 '12 at 16:33
  • 1
    @JamesMcLaughlin I strongly disagree with semicolons being "completely stylistic" – Adriano Carneiro Nov 13 '12 at 16:34
  • The problem is a simple typo. Separately: It's best not to rely on the horror that is [automatic semicolon insertion](http://ecma-international.org/ecma-262/5.1/#sec-7.9). Eventually, it ***will*** bite you. – T.J. Crowder Nov 13 '12 at 16:37
  • 1
    @Adrian, while you may disagree, this is absolutely no NEED to use semicolons in javascript. (I do use them by the way, maybe because my primary language is C#?) – gdoron Nov 13 '12 at 16:37
  • @gdoron: Sure there is. Maintainability is a need. But I don't want to swamp the question with off-topic comments... :-) – T.J. Crowder Nov 13 '12 at 16:37
  • @gdoron Look here for some ambiguous code due to lack of semicolons: http://stackoverflow.com/a/1169596/570191 – Adriano Carneiro Nov 13 '12 at 16:39
  • @T.J.Crowder. See my answer and [this little demo](http://jsfiddle.net/pujk5/) ... :) – gdoron Nov 13 '12 at 16:39
  • @gdoron: I should have looked more closely, stray parens are frequently not actually syntactically wrong. :-) Nice one. – T.J. Crowder Nov 13 '12 at 16:41
  • 1
    @gdoron Even more fun than that: http://jsfiddle.net/28yX3/ – James M Nov 13 '12 at 16:44
  • 1
    @gdoron Since you use them, you won't have been bitten by *not* using them. Relying on getting the ambiguous cases right is rarely the right answer, and leads to inconsistent coding style. While it wasn't the issue in this case, why risk it *being* the issue? – Dave Newton Nov 13 '12 at 16:45

4 Answers4

2

You got typo with the braces:

} else if (theSibling == 'keywords')(
       $(this).before('the string is keywords')
       keyTot++
    )

Should be:

} else if (theSibling == 'keywords'){
       $(this).before('the string is keywords')
       keyTot++
    }

The curly braces are mandatory for more than one command, this is why you're not getting the error.

When you used regular braces the code was treated as:

} else if (theSibling == 'keywords')
    ($(this).before('the string is keywords'))

Which is valid, but useless...

gdoron
  • 147,333
  • 58
  • 291
  • 367
1
else if (theSibling == 'keywords')(
        $(this).before('the string is keywords')
        keyTot++
)

You wanted curly braces:

else if (theSibling == 'keywords'){
        $(this).before('the string is keywords')
        keyTot++
}
James M
  • 18,506
  • 3
  • 48
  • 56
0

You are using regular brace where you should be using curly braces. Proper sintax for if and else is if(){} else if () {} So your both blocks are wrong.

tonino.j
  • 3,837
  • 28
  • 27
-1

You are missing semicolons after the $this.before(); statements.

Coby
  • 1,528
  • 9
  • 8