Search code examples
jquerywarningsselectorrubymine

RubyMine Waring Message - "Duplicated jQuery selector"


I have add some simple JavaScript/jQuery code to my Rails application which is making RubyMine to display the following warning:

"Duplicated jQuery selector".

The environment gives me additional information about the warning message:

Checks that jQuery selectors are used in an efficient way. It suggests to split descendant selectors which are prefaced with ID selector and warns about duplicated selectors which could be cached.

The code itself is pretty simple and I am using it for years to check if an element is rendered, if it is not found, the element is added in the page:

if($('#error').length == 0) {
    $('<span class="error" id="error">'+error_message+'</span>')
            .insertAfter('#element');
}else {
   $('#error').html(error_message);
}

The environment is giving me the error above for the following line:

$('#error').length == 0

I have used this for a long time but google it again to see if there is a way to rewrite it, the only thing that I have found is to use just:

$('#error').length

But the environment show me the same warning again.

Is really there someway to optimized this and I am not using it correctly or the warning could be result of some internal misunderstand by the environment rules?


Solution

  • The warning has nothing in common with wrong or slow jQuery syntax of selector. I have been using the same selector in the current file and the RubyMine environment is saying that this can be optimized by making reference to this selector, as follows:

    var selector = $(jquery_selector)