42

I am currently trying to set up a table with 6 clickable cels that allow for a input box to appear so you can add comments but I am getting a duplicated jQuery selector error and also through debugging my second function I found that .html() is not working either. Here is my code for the 6 functions; each of which are called when a specific cell is clicked:

$("#mondayCommentLink").click(function (){
    var mondayhtmls = $("#mondayComment");
    var input = $("<input type='text' id='mondayCommentText' name='mondayCommentText'  />");
    input.val(data.days[0].comment);
    mondayhtmls.html(input);
});

$("#tuesdaysCommentLink").click(function (){
    var tuesdayhtmls = ("#tuesdayComment");
    var inputt = $("<input type='text' id='tuesdayCommentText' name='tuesdayCommentText' />");
    inputt.val(data.days[1].comment);
    tuesdayhtmls.html("test");
});

$("#wednesdayCommentLink").click(function (){
    var htmls = ("#wednesdayComment");
    var input = $("<input type='text' id='wednesdayCommentText' name='wednesdayCommentText' />");
    input.val(data.days[2].comment);
    htmls.html(input);
});

$("#thursdayCommentLink").click(function (){
    var htmls = ("#thursdayComment");
    var input = $("<input type='text' id='thursdayCommentText' name='thursdayCommentText' />");
    input.val(data.days[3].comment);
    htmls.html(input);
});

$("#fridayCommentLink").click(function (){
    var htmls = ("#fridayComment");
    var input = $("<input type='text' id='fridayCommentText' name='fridayCommentText' />");
    input.val(data.days[4].comment);
    htmls.html(input);
});

$("#saturdayCommentLink").click(function (){
    var htmls = ("#saturdayComment");
    var input = $("<input type='text' id='saturdayCommentText' name='saturdayCommentText' />");
    input.val(data.days[5].comment);
    htmls.html(input);
});

And this is where they are called from:

  <th id="mondayComment" name="mondayComment" style="text-align: center; width: 115px;"><div id="mondayCommentLink">+</div></th>
  <th id="tuesdayComment" name="tuesdayComment" style="text-align: center; width: 115px;"><div id="tuesdaysCommentLink">+</div></th>
  <th id="wednesdayComment" name="wednesdayComment" style="text-align: center; width: 115px;"><div id="wednesdayCommentLink">+</div></th>
  <th id="thursdayComment" name="thursdayComment" style="nowrap; text-align: center; width: 115px;"><div id="thursdayCommentLink">+</div></th>
  <th id="fridayComment" name="fridayComment" style="text-align: center; width: 115px;"><div id="fridayCommentLink">+</div></th>
  <th id="saturdayComment" name="saturdayComment" style="text-align: center; width: 115px;"><div  id="saturdayCommentLink">+</div></th> 

I don't understand why I am getting a duplicate selector error on #mondayCommentLink, #tuesdayCommentLink, etc. Is there something I'm missing or mistakenly doing wrong? The first cell works and I can click it and a input box will pop up but it fails upon the second cell #tuesdayCommentLink at the line tuesday.htmls.html("test");.

Barmar
  • 741,623
  • 53
  • 500
  • 612
thehoule64
  • 1,761
  • 5
  • 15
  • 22
  • 3
    I've never heard of a "duplicate selector error", are you sure it isn't a warning from some other js file you're including that is validating your jQuery usage? – Kevin B May 31 '13 at 18:47
  • I can't find any mention of such an error in the jQuery source code. – Barmar May 31 '13 at 18:48
  • Can you post the complete error message that you see in the console? – Selvakumar Arumugam May 31 '13 at 18:48
  • 1
    BTW, all that repetition is ugly. Please use classes so you can write it in DRY manner. – Barmar May 31 '13 at 18:51
  • There's no error message in the console, it's just highlighted and displays `Duplicated jQuery Selector` and that's all. `#mondayCommentLink` function works fine `#tuesdayCOmmentLink` function and any other hang at the `.html` line at the bottom. – thehoule64 May 31 '13 at 18:52
  • 1
    Your also missing the `$` before you are setting the `var htmls` – Blake Plumb May 31 '13 at 18:53
  • What other plugins are you using? – Barmar May 31 '13 at 18:53
  • `var tuesdayhtmls = ("#tuesdayComment");` should be `var tuesdayhtmls = $("#tuesdayComment");` – Abraham Uribe May 31 '13 at 18:53
  • 1
    There is nothing that comes with a browser that would automatically highlight code and print `Duplicated jQuery Selector`. You must have something installed that is doing that. – Kevin B May 31 '13 at 18:54
  • The missing `$` should result in a console error like `TypeError: Object #tuesdayComment has no method 'html'` – Barmar May 31 '13 at 18:55
  • 1
    I'm using IntelliJ, that is what's highlighting and displaying the error – thehoule64 May 31 '13 at 19:00
  • Adding the `$` in `var tuesdayhtmls = ("#tuesdayComment");` still does not allow the input box to be displayed. – thehoule64 May 31 '13 at 19:05

4 Answers4

105

There is no such a Duplicated jQuery Selector error; that's a warning thrown by IntelliJ (and other IDEs from idea like WebStorm...) suggesting that you should store your jQuery selection in a local variable rather than repeating the selection.

Excerpt from jQuery documentation:

Saving Selections

jQuery doesn't cache elements for you. If you've made a selection that you might need to make again, you should save the selection in a variable rather than making the selection repeatedly.

1| var divs = $( "div" );

Once the selection is stored in a variable, you can call jQuery methods on the variable just like you would have called them on the original selection.

A selection only fetches the elements that are on the page at the time the selection is made. If elements are added to the page later, you'll have to repeat the selection or otherwise add them to the selection stored in the variable. Stored selections don't magically update when the DOM changes.

However, in your code there is no duplicated jQuery selection so I bet that warning is coming from somewhere else.. What is in line with the fact that the error persists after adding the missing $.

In general is a good practice to add the reported error to your questions..

yiwei
  • 4,022
  • 9
  • 36
  • 54
aebmad
  • 1,390
  • 1
  • 15
  • 14
13

The "duplicate selector" is indeed a JS lint warning, that you'll see in IDEs like PHPStorm / WebStorm. For performance reasons, you'll want to cache your selectors whenever possible.. e.g.:

(function($) {
  var 
    $mondayCommentLink = $("#mondayCommentLink"),
    $mondayHtmls = $("#mondayComment"),
    $inputMonday = $("<input type='text' id='mondayCommentText' name='mondayCommentText'  />");

    $mondayCommentLink.on('click', function() {
      $inputMonday.val(data.days[0].comment);
      $mondayHtmls.html($inputMonday);
    });

})(jQuery);

.. and so on. I just did Monday, but you'd continue to add a variable reference to the selector that you can grab that's already in memory. Things are a bit more complicated when dealing with AJAX or if you have multiple scopes, but this is the basic idea. It's just convention, but I find it easier to reference selectors with var $elem and camelcased.

JDev518
  • 772
  • 7
  • 16
5
Duplicate jQuery Selector

is a warning or hint from JSHint. It is not an error.

If you select a DOM element multiple times without a block of code in JavaScript or jQuery, this warning is shown.

For best practice, you should assign it to a variable like this:

let $myInput = jQuery('.first-name');

in that way, you can use $myInput for further codes.

Gideon Babu
  • 336
  • 2
  • 5
  • 11
3

I had this same error trying something like this:

if  ($("#checkbox").is(':checked')){
   value = $("#checkbox").val();

For some reason, the error was thrown on "#checkbox". I solved the problem by including:

//noinspection JSJQueryEfficiency at the top of the if statement like this:

 //noinspection JSJQueryEfficiency
 if  ($("#checkbox").is(':checked')){
   value = $("#checkbox").val();

and the error message went away.

Joern Boegeholz
  • 533
  • 1
  • 8
  • 25
Ojonugwa Jude Ochalifu
  • 26,627
  • 26
  • 120
  • 132
  • 11
    The problem here is that you are calling the selector every time you do `$("#checkbox")`. You can do `var myCheckbox = $("#chcekbox");` to cache the result. Which as the warning says "JSJQueryEfficiency" is not effective to select it every time. – Athiwat Chunlakhan Nov 06 '14 at 09:03
  • 3
    All this does is hide the issue, not fix it – Stephen R Jul 18 '19 at 20:12