General Question

GrayTax's avatar

How would you form an if statement in a jQuery function, then call that function using a .click() handler?

Asked by GrayTax (546 points ) February 9th, 2012

I seem to be having trouble making this work:

I need to be able to perform a check on a css property of a particular element, and get the JavaScript to perform an animation based on the returned value.

The animation is working fine on its own, I just can’t seem to get the conditional to work; I’m convinced I have the syntax wrong or something but Dreamweaver isn’t throwing up any errors.

Any help is appreciated, and I can provide the script (as it currently is) if needed. Also I’m a complete novice at this…

Many thanks, jellies :D

Observing members: 0 Composing members: 0

6 Answers

phaedryx's avatar

Can you show us what your current code looks like? Perhaps you could paste it to http://jsfiddle.net/ or https://gist.github.com/ and give us a link? It’s easier answer if we’re referencing the same things.

GrayTax's avatar

This should be the file. Thanks for the link to github; I didn’t know about it before now!

phaedryx's avatar

Huh, that’s kinda hard to read; here’s the same code with syntax highlighting and after going through a beautifier.

I’m currently at work. I’ll take a look later tonight.

GrayTax's avatar

After working on it for a little bit tonight I managed to get it working. I’ve updated the gist to reflect the changes.

I’m not sure how to get the syntax highlighting on it though, sorry

funkdaddy's avatar

@GrayTax – you got there indirectly and glad it’s working.

The easiest way is probably just to wrap the call to your function in a generic function rather than all the code. Just to make it a little cleaner.

So something like

function stuff() {
// do stuff
}

$(’#basketlink’).click(function() {
stuff();
});

Cool?

GrayTax's avatar

@funkdaddy Ah, good idea. I had tried doing that before but because the clauses in the if/else statement didn’t work (which I didn’t realise at the time) it failed. I gave up and eventually got it working as you saw, but completely forgot about making it cleaner!

I’ll be changing it tonight, so thanks! =]

Answer this question

Login

or

Join

to answer.

This question is in the General Section. Responses must be helpful and on-topic.

Your answer will be saved while you login or join.

Have a question? Ask Fluther!

What do you know more about?
or
Knowledge Networking @ Fluther