0

After looking at jQuery selector with variable in the selector and a few other posts, I'm not finding an answer to my question, which makes me think maybe I'm maybe going about it wrong.

I have a .car_ul li element being passed in as 'el',

I'm trying to operate on $('.car_ul:not(:animated)') using $(':not(:animated)',adult) - wondering how I can do that?

slideLeft: function(el) {

                var adult = el.parent.attr('class')
                var itemWidth = el.outerWidth() + 10
                var leftIndent = parseInt(adult.css('left')) + itemWidth


        $('.car_ul:not(:animated)').animate({'left' : leftIndent}, 500,
                        function() {
                            $('.car_ul li:first').before($('.car_ul li:last'))
                            $(adult).css({'left':'-160px'})
                        })

            },
7
  • the property is named className, FYI. the object version of adult should have worked. Provide html required to recreate your problem and we may be able to help further. Commented Mar 10, 2014 at 19:02
  • Or else use .attr('class'). Commented Mar 10, 2014 at 19:04
  • @Scimonster - don't use jQuery unless needed. el.parentNode.className works just fine, and much faster. Commented Mar 10, 2014 at 19:05
  • Are you trying to target .car_ul elements or descendants of the .car_ul elements. Also, it'd probably be a lot easier to understand if you just posted your code instead of trying to talk through it. Commented Mar 10, 2014 at 19:07
  • Sorry, posted code so you can actually see what I'm doing. Need to be better about doing that in the first place. Commented Mar 10, 2014 at 19:27

1 Answer 1

1

Since you asked for possible optimization points:

slideLeft:function($el){
    var $adult = $('.'+$el.get(0).parentNode.className),
        itemWidth = $el.outerWidth() + 10,
        leftIndent = parseInt($adult.css('left'),10),
        $menu = $('.car_ul'),
        $li = $menu.find('li');

    $menu.not(':animated').animate({left:leftIndent},500,function(){
        $li.first().before($li.last());
        $adult.css({left:-160});
    });
},
  • since you really need the adult object, capture that in a variable as an object
  • use base 10 for parseInt function (always do this)
  • cache .car_ul and its respective li
  • use the .not() method to leverage caching of .car_ul
  • use the .first() and .last() methods to leverage caching of li
  • use the DOM names and numeric values rather than the string values for CSS assignments

I also renamed your el variable to $el ... this is more stylistic, but its generally considered best practice to call out jQuery objects vs DOM elements with the naming convention.

Hope this helps!

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

1 Comment

Definitely! All the pointers are much appreciated, as I'm just getting into using the module pattern, and also not great at JS/jQuery.

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.