0

I have a tiny script I'm working on :)

The idea is if the span tag just contains autocompleteitem and not autocompleteitem autocompleteitemwithcontainer then it'll display the word hello. Right now it displays hello for both :(

Demo: https://jsfiddle.net/L33mqqtp/

$(document).ready(function() {
          $(document).on('click','.autocompleteitem',function(){
            $("div").show();
        });
    });

How do I update my code to do this?

4 Answers 4

3

Try this

'click','.autocompleteitem:not(.autocompleteitemwithcontainer)'
Sign up to request clarification or add additional context in comments.

1 Comment

Wow - amazing. Thank you, Ilya :-)
1

They both have .autocompleteitem class! To obtain what you want, use a different selector, which only applies to first one:

$(document).ready(function() {
  $(document).on('click','.autocompleteitem:not(".autocompleteitemwithcontainer")',function(){
    $("div").show();
  });
});

Comments

1

try this

$(document).ready(function() {
    $("span").on('click', function(){
        if(this.className == "autocompleteitem")
            $("div").show();
     });
});

Comments

1

Use the attribute selector syntax. That will allow you to directly match an entire attribute. This way you don't have to make a bunch of different combinations for each thing that you don't want to be selected.

Example selector: jQuery('[class="autocompleteitem"]')

Working Example:

$(document).ready(function() {
          $(document).on('click','[class="autocompleteitem"]',function(){
            $("div.containertoshow").toggle();
        });
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<a href="#" class="autocompleteitem">Should work</a><br>
<a href="#"class="autocompleteitem autocompleteitemwithcontainer">Should not work</a>

<div class="containertoshow" style="display:none;">That button does toggle me on/off!</div>

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.