Dan
Dan

Reputation: 550

Why does this jQuery work as a fiddle but not on my web page?

I've for this fiddle ( http://jsfiddle.net/hgGfw/ ) , I've been following a slider tutorial and that's the code I've got. It's using a self invoking function ( I think that's what it's called??). Anyway, It works with the jQuery set to "onLoad" on the fiddle, but when it's on my server it does nothing until it's wrapped in $(document).ready(.... script. Why is that? JSLint also moans about this;

( direction === 'next' ) ? ++current : --current;

Any idea why? My PHP isn't bad, so I know the syntax for that statement and it looks fine to me.

Thanks, Dan.

Upvotes: 0

Views: 346

Answers (2)

freefaller
freefaller

Reputation: 19953

If you place javascript / jquery into the JavaScript panel of a http://jsfiddle.net, it will automatically place your code into a $(window).load(function(){ } block (when the Framework dropdown is set to onLoad and a jQuery option).

If you have a look at the frame source of the result pane, you should see this is the case.

With regards to why JSLint doesn't like the command, I guess it is expecting an assignment (despite the fact that you are doing incrementation/decrementation on the current variable).

If you assign the value to a variable, JSLint is happy. I.e...

var tempVar = ( direction === 'next' ) ? ++current : --current;

Upvotes: 2

OlduwanSteve
OlduwanSteve

Reputation: 1295

I suspect JSLint is complaining because the ternary operator is intended to select a value for an expression, rather than select an execution path.

Here you've used it as shorthand for an 'if' statement, which will work but is a little unusual.

Upvotes: 1

Related Questions