Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to search at element's value?

Here is my code:

if($('span').text().indexOf("t")){
  console.log($('span').text());
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span>one</span>
<span>two</span>
<span>three</span>
<span>four</span>
<span>five</span>
<span>six</span>

The result should be just three and two in console. Because just they contain t letter. But as you see all values will be shown in the console.

As I've mentioned, I'm trying to make a small search engine for a autocomplete box. How can I fix it?

like image 541
stack Avatar asked Jan 26 '17 19:01

stack


People also ask

How do I find an element with a specific ID?

The getElementById() method returns an element with a specified value. The getElementById() method returns null if the element does not exist. The getElementById() method is one of the most common methods in the HTML DOM. It is used almost every time you want to read or edit an HTML element.

How do you find the text value of an element?

Use the textContent property to get the text of an html element, e.g. const text = box. textContent . The textContent property returns the text content of the element and its descendants.

How do you find the element of an element?

You get the element with id="foo" . You then find the objects that are contained within that object that have class="bar" . That returns an array-like nodeList, so you reference the first item in that nodeList. You can then set the innerHTML of that item to change its contents.

Can I get element by value in JavaScript?

Introduction to JavaScript getElementsByName() method The getElementsByName() accepts a name which is the value of the name attribute of elements and returns a live NodeList of elements. The return collection of elements is live.


5 Answers

You can to use contains selector:

$('span:contains("t")').each(function(){
  console.log($(this).text());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span>one</span>
<span>two</span>
<span>three</span>
<span>four</span>
<span>five</span>
<span>six</span>
like image 176
BrTkCa Avatar answered Sep 28 '22 15:09

BrTkCa


As per the documentation text() method returns a string containing the combined text of all matched elements that what it showing in your console, instead you need to iterate over them. Even though your condition is wrong, which only fails when the index is 0(0 is falsy value and all other integer values are truthy in Javascript), it should be .indexOf("t") > -1.

$('span').each(function() {
  if ($(this).text().indexOf("t") > -1)
    console.log($(this).text());
})

// or use text() method with callback which holds 
// old value as second argument and iterates internally
$('span').text(function(i, txt) {
  if (txt.indexOf("t") > -1)
    console.log(txt);
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span>one</span>
<span>two</span>
<span>three</span>
<span>four</span>
<span>five</span>
<span>six</span>

The simplest way is to use CSS :contains() pseudo-class selector and iterate over the elements using each() method.

$('span:contains("t")').each(function() {
  console.log($(this).text());
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span>one</span>
<span>two</span>
<span>three</span>
<span>four</span>
<span>five</span>
<span>six</span>

UPDATE : You can use map() method with get() method to get as an array.

console.log($('span:contains("t")').map(function() {
  return $(this).text();
}).get())
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span>one</span>
<span>two</span>
<span>three</span>
<span>four</span>
<span>five</span>
<span>six</span>
like image 39
Pranav C Balan Avatar answered Sep 28 '22 16:09

Pranav C Balan


$('span') selects all the span elements on the page. If you need to work with them individually, you need to loop over all the span elements and test the condition on individual items. You can use .each function for this.

Also, indexOf returns -1 if the string doesn't contain the argument, so you need to compare it with -1.

$('span').each(function () {
  if($(this).text().indexOf("t") != -1){
     console.log($(this).text());
  }
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span>one</span>
<span>two</span>
<span>three</span>
<span>four</span>
<span>five</span>
<span>six</span>
like image 32
Aᴍɪʀ Avatar answered Sep 28 '22 15:09

Aᴍɪʀ


You should loop through the elements and check the values using > operator:

$('span').each(function() {
  if ($(this).text().indexOf("t") > -1) {
    console.log($(this).text());
  }
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span>one</span>
<span>two</span>
<span>three</span>
<span>four</span>
<span>five</span>
<span>six</span>

You can read more about each() method here.

like image 45
Ionut Avatar answered Sep 28 '22 15:09

Ionut


$('span').text() return linked text of every span in array, so you must iterate through span elements with for, or each (jQuery) method:

 $('span').each(function () {
    if($(this).text().indexOf("t") > 0){
        console.log($(this).text());
    }
 });

 var elems = $('span');
 for(var i=0; i<elems.length; i++){
     if($(elems[i]).text().indexOf("t") > 0){
        console.log($(this).text());
    }
 }
like image 42
Nedim Hozić Avatar answered Sep 28 '22 16:09

Nedim Hozić