-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 is supporting Monica
  • 142,542
  • 55
  • 282
  • 355
Ar77
  • 1,203
  • 2
  • 19
  • 29

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 is supporting Monica
  • 142,542
  • 55
  • 282
  • 355
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 McLaughlin
  • 18,774
  • 3
  • 47
  • 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,729
  • 26
  • 26
-1

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

Coby
  • 1,519
  • 9
  • 8