0

I'm trying to shorthand my jQuery now that I have my working script and i'm doing (where necessary) inline IF statements to shorten code. Only issue is i'm trying to do so without using an ELSE statement as it is not required, but this is just throwing me an error.

jQuery IF Statement

(conH > sliderH ? ($(".sliderDownDisabled").addClass("sliderDown") $(".sliderDown").removeClass("sliderDownDisabled")));

The error in the error console is telling me I missing parentheses, which I'm not as i have checked through the line and all the matching parentheses are correct. I don't know where I am going wrong and if anybody could point me in the right direction that would be great.

Solution

(conH > sliderH && ($(".sliderDownDisabled").addClass("sliderDown") && $(".sliderDown").removeClass("sliderDownDisabled")));

By adding the && in place of the ? selector and inbetween the two pieces of code I want to be executed I have obtained the desired results.

1

1 Answer 1

1

You can us && (second statement is executed only if conH > sliderH is true)

&& needs to be used in place of the ? selector and also in between the two statements.

(conH > sliderH && ($(".sliderDownDisabled").addClass("sliderDown") && $(".sliderDown").removeClass("sliderDownDisabled")));

Simple Demo ---> http://jsfiddle.net/jJWs2/6/

Sign up to request clarification or add additional context in comments.

2 Comments

This hasn't done anything to solve my issue unfortunately. Still tells me I'm missing parentheses.
However, by adding && into the statement aswell as replacing the ? selector it has solved my issue. I will upvote for helping provide the answer.

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.