3

I have an html structure like this:

<div class="test">
   <span class="content">1</span>
</div>
<div class="test">
   <span class="content">2</span>
</div>
...
<div class="test">
   <span class="content">100</span>
</div>

In my javascript code, I need to get an <span> element with class content that has exactly 1 or 2 , ..., 100

I tested jquery .contains method, but this returns all elements that have for example 1. such as 1, 12, ....

1
  • First, there is no span element with class test. Second ... is a text node and is not wrapped under any element Commented Apr 18, 2017 at 5:53

5 Answers 5

4

You can use filter method which accepts a callback function applied to every item.

var array=$('.test').find('.content').filter(function(){
   return $(this).text().trim()==100;
});
console.log(Array.from(array));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="test">
   <span class="content">1</span>
</div>
<div class="test">
   <span class="content">2</span>
</div>
<div class="test">
   <span class="content">100</span>
</div>

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

4 Comments

Instead of .html use .text.
@Rajesh It really depends. In this case with respect to the question, both are functionally interchangeable.
@Alexandru-IonutMihai Thank you for your perfect answer.
@Terry In my understanding, we create a sample for SO but our actual code is way different. Hence I'd prefer using .text instead
0

You can proceed in the following manner:

$('.content').each(function(){
  if($(this).html() == "2")
  {
    console.log("THE SPAN WITH 2 IS ");
    console.log($(this)[0]);
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="test">
   <span class="content">1</span>
</div>
<div class="test">
   <span class="content">2</span>
</div>
<div class="test">
   <span class="content">100</span>
</div>

What we do here is check through the content class of the spans and check if their inner html is 2 and if it is we console.log it.

Comments

0

You can use the vanilla .indexOf() method.

The indexOf method takes a parameter of the string you want to find and returns either the index (if it's found), or -1 if it's not.

var myEl = document.querySelector(".test"):

for loop...

if( myEl.innerHTML.indexOf(2) != -1 ){
    console.log("This element contains the number 2")
}

Comments

0

You can use .filter(), get and check .textContent or .innerHTML of element, at .filter() callback you can use RegExp N$ where N is number to match. For example, to match elements having "1" or "2" set at .textContent you can use RegExp /1$|2$/; to match "100", /100$/; with RegExp.prototype.test()

var filtered = $("span.content").filter((_, {textContent}) => 
                 /1$|2$/.test(textContent));

filtered.css("color", "green");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">
</script>
<div class="test">
   <span class="content">1</span>
</div>
<div class="test">
   <span class="content">2</span>
</div>
...
<div class="test">
   <span class="content">100</span>
</div>

Comments

-1

You can do something like this..

$('.test').each(function() {

if($(this).text == '1')
{
var a = $(this).html();
}

});

a will now contain the html of that span which contains your text.

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.