0

There has to be a better way to set the variable $callLinkContainer in the following code block:

var $callLinkContainer;
if ( $callLink.closest('class1-tel').length > 0 ) {
     $callLinkContainer = $('body').find('.class1-container');
} else if ( $callLink.closest('.class2').length > 0 ) {
     $callLinkContainer = $('body').find('.class2-container');
} else {
     $callLinkContainer = $callLink.closest('.class3');
}

Previously, I had used a ternary operator to set the variable, but I'm pretty sure the third condition makes that impossible. I'd like to make this code more concise if it's possible to do so.

2
  • Why do you need $('body').find(...) in the first two assignments? Commented Apr 29, 2014 at 1:35
  • This is just a snippet from a much larger piece of code. I'm basically looking for data-attributes on containers of links and setting the variable to that value. Commented Apr 29, 2014 at 1:37

4 Answers 4

2

You can use several ternary operators with indentation

var $callLinkContainer = $callLink.closest('class1-tel').length > 0 
  ? $('body').find('.class1-container')
  : $callLink.closest('.class2').length > 0 
    ? $('body').find('.class2-container')
    : $callLink.closest('.class3');

The priority of the operators is so that you don't need to add parenthesis like in

a ? b : (c ? d : e)

You might want to add them for extra clarity, though.

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

Comments

1

You can nest conditional operators. The code is shorter, and with appropriate line breaks and indentation it's as readable as the if statements.

$callLinkContainer = $callLink.closest('class1-tel').length ?
    $('.class1-container') :
    ($callLink.closest('.class2').length ?
        $('.class2-container') :
        $callLink.closest('.class3'));

9 Comments

FWIW, you can remove the > 0s
You can safely break this into multiple lines to improve readability - semicolon inference won't get in the way.
I did break it into multiple lines.
OK, now I've broken it into more lines -- it is indeed more readable.
Please don't do that even if it is on multiple lines. That's horribly unreadable. There's nothing wrong with a few if statements if it dramatically simplifies your code.
|
0

I'd like to do the next suggestion

  1. Encapsulate the code in several functions

    function isCallLinkClosest(var callLinkSelector){ 
        return $callLink.closest(callLinkSelector).length > 0;
    }
    
    function getCallLinkContainer(var containerSelector){
        return $('body').find(containerSelector);
    }
    
    function getDataAttributeByClass(){ 
        if ( isCallLinkClosest('class1-tel')) {
         return getCallLinkContainer('.class1-container');
        } else if ( isCallLinkClosest('.class2')) {
         return getCallLinkContainer('.class2-container');
        } else {
         return $callLink.closest('.class3');
        }
    }
    

This is the best suggestion that I can do with the given information :)

Comments

0

Do .class1-tel, .class2 or .class3 exist at the same time?
If not, then you could possibly do something like this:

var $callLinkContainer = ($callLink.closest('.class1-tel, .class2').length
    ? $('body').find('.class1-container, .class2-container')
    : $callLink.closest('.class3'));

If you could be more specific about class names and dom structure, the code could be even easier.

Comments

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.