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.

Banny
  • 811
  • 4
  • 13
  • 36
  • 1
    Check this out: http://stackoverflow.com/questions/11554492/javascript-shorthand-if-statement-without-the-else-portion – JAKEtheJAB Jun 19 '13 at 13:39

1 Answers1

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/

Adil Shaikh
  • 44,509
  • 17
  • 89
  • 111
  • 1
    This hasn't done anything to solve my issue unfortunately. Still tells me I'm missing parentheses. – Banny Jun 19 '13 at 13:39
  • However, by adding `&&` into the statement aswell as replacing the `?` selector it has solved my issue. I will upvote for helping provide the answer. – Banny Jun 19 '13 at 13:42