Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jetbrains PHPStorm 5.0.4 tells me I have a Duplicated jQuery Selector

This is the piece of jQuery I wrote,

$('#editUser').click(function() {
    if ($(".selectedTR")[0]) {
        if($('.form-actions').is(':visible')) {
            $('.form-actions').slideUp('slow',function() {
                $('.form-actions > h3').text("Edit");
            }).css('display', 'none');
        }

        $('.form-actions').css('display', 'block').slideDown('slow');
    } else {
        alert("Please select a user");
    }
});

How can I remove the duplicated selectors?

like image 499
R4VANG3R Avatar asked Dec 04 '12 14:12

R4VANG3R


1 Answers

You can cache the selector by putting it in a variable. Try this:

$('#editUser').click(function() {
    if ($(".selectedTR").length) { 
        var $formActions = $('.form-actions');
        if ($formActions.is(':visible')) {
            $formActions.slideUp('slow', function() {
                $formActions.children('h3').text("Edit");
            }).css('display', 'none');
        }
        $formActions.css('display', 'block').slideDown('slow');
    } else {
        alert("Please select a user");
    }
});
like image 122
Rory McCrossan Avatar answered Oct 21 '22 14:10

Rory McCrossan