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?
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.
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.
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.
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.
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>
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>
$('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>
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.
$('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());
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With