0
   $("#whoToFollow").on("click", "p", function() {
    const text = $(this).text()
   $("div:not(:contains(' + text + '))").remove()
  });

I want to remove any divs which does not contain the text in the p tag that was clicked. The above code works if I replace (' + text + ') with the exact text ('david') but does not work when using the text variable. What am I missing here?

1
  • 2
    try $("div:not(:contains('" + text + "'))").remove() You have to escape the " to include your variable Commented Feb 9, 2018 at 12:51

1 Answer 1

1

Add double quotes in your jquery selector...

 $("#whoToFollow").on("click", "p", function() {
    const text = $(this).text()
   $("div:not(:contains('" + text + "'))").remove()
  });
Sign up to request clarification or add additional context in comments.

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.