0

I'm trying to do is when I click on a text, if another div has a child that contains this text fade in. but it's not working

$('.rel-head').click(function() {

  if ($('.art-h .head-art'): contains($(this).text())) {
    $(this).parent().fadeIn().siblings().fadeOut();
  }

});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="art-h">
  <div class="head-art">some text</div>
</div>
<div class="art-h">
  <div class="head-art">anoter text</div>
</div>
<div class "related-heads">
  <h1 class="rel-head">some text</h1>
</div>

13
  • 1
    Need the html as well. Also you have :contains not .contains Commented Feb 5, 2019 at 23:31
  • for :contains use $('.art-h .head-art:contains($(this).text()')) Commented Feb 5, 2019 at 23:39
  • if($('.art-h .head-art').text() == $(this).text()) { ... } Commented Feb 5, 2019 at 23:53
  • <div class="art-h"> <div class="head-art">some text</div> </div> <h1 class="rel-head">some text</h1> @basic Commented Feb 5, 2019 at 23:57
  • @mark.hch but how to select this one $('.art-h .head-art') if there is more than one have the same class? Commented Feb 5, 2019 at 23:59

2 Answers 2

1
  1. Use selector :contains and concat the string properly.

$('.rel-head').click(function() {

  if ($('.art-h .head-art:contains('+$(this).text()+')')) {
    $(this).parent().fadeIn().siblings().fadeOut();
  }

});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="art-h">
  <div class="head-art">some text</div>
</div>
<div class="art-h">
  <div class="head-art">anoter text</div>
</div>
<div class "related-heads">
  <h1 class="rel-head">some text</h1>
</div>

Sign up to request clarification or add additional context in comments.

Comments

0

It's not very efficient to search the entire body every time you click on each of the specified items.

This is what I'd do.

let headArtArray = $('.art-h .head-art'); //Assign it to a variable early to reduce searching

$('.rel-head').click(function() {
  let el = $(this);
  let text = el.text();

  for (let i = 0; i < headArtArray.length; i++) {
    
    if (headArtArray[i].innerText.includes(text)) {
      el.parent().fadeIn();
    } else {
      $(headArtArray[i]).fadeOut();
    }
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="art-h">
  <div class="head-art">some text</div>
</div>
<div class="art-h">
  <div class="head-art">anoter text</div>
</div>
<div class "related-heads">
  <h1 class="rel-head">some text</h1>
</div>

8 Comments

it's not working and the console is giving me an error in this line if (headArtArray[i].text().includes(text)) {
@IbrahimYousry I'll fix that
@IbrahimYousry Fixed, just changed text() to innerText.
if($('.art-h .head-art').text() == $(this).text()) { ... } if it like this will it work, and if it will how can I select this specific $('.art-h .head-art')
The code snippet posted above does work as expected. Double check what you have to ensure it is consistent.
|

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.