Reputation:
I want to return false
if any of mdpoptags
has text as variable a
.
I want do this without each
loop.
var a = 'ABBA'
if ($('.mdpoptag:contains(' + a + ')').length > 0) {
console.log('323');
//return false;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class='mdpoptag'>ABBA</div>
<div class='mdpoptag'>ABB</div>
<div class='mdpoptag'>AB</div>
Problem: if a
is ABB
or AB
- result is true.
I need to match the entire a
string with entire mdpoptags
text.
Is it possible using contains
, or include
... just without each
loop?
Upvotes: 0
Views: 62
Reputation: 10081
I've added some other examples to make it more generic.
Let's say the action you want to execute on all your matched elements is .hide()
, to make it simple.
Snippet using :contains()
:
var a = 'ABBA';
// If you want to execute a global action on all your elements, you can do the following:
var matched = $('.mdpoptag:contains(' + a + ')');
if (matched.length) {
matched.hide();
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class='mdpoptag'>ABBA</div><!-- ABBA is here -->
<div class='mdpoptag'>ABB</div>
<div class='mdpoptag'>AB</div>
<div class='mdpoptag'>ABBABB</div>
<div class='mdpoptag'>BBABBA</div>
<div class='mdpoptag'>ABBA</div><!-- ABBA is here too -->
The problem here is that :contains()
matches "ABBABB" and "BBABBA" too.
⋅ ⋅ ⋅
So, you may want to use .filter()
instead:
var a = 'ABBA';
var matched = $('.mdpoptag').filter(function() {
return $(this).text() === a;
});
if (matched.length) {
matched.hide();
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class='mdpoptag'>ABBA</div><!-- ABBA is here! -->
<div class='mdpoptag'>ABB</div>
<div class='mdpoptag'>AB</div>
<div class='mdpoptag'>ABBABB</div>
<div class='mdpoptag'>BBABBA</div>
<div class='mdpoptag'>ABBA</div><!-- ABBA is here too -->
Documentation about .filter()
: http://api.jquery.com/filter/
⋅ ⋅ ⋅
Also, note that if you want to console()
something for each element, a loop is mandatory.
Hope it helps.
Upvotes: 1
Reputation: 3854
I'm just guessing now becuase in my opinion the answer to your question is already in your question.
If you want to display only those divs that does not contain variable a
value then you can do it like this
var a = 'ABBA',
container = $('.mdpoptag:contains(' + a + ')');
if (container.length > 0) {
container.hide();
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class='mdpoptag'>ABBA</div>
<div class='mdpoptag'>ABB</div>
<div class='mdpoptag'>AB</div>
Upvotes: 1
Reputation: 3919
You had a spelling error in your variable a content.
var a = 'ABBA';
if ($('.mdpoptag:contains(' + a + ')').length > 0) {
console.log('found!');
//return false;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class='mdpoptag'>ABBA</div>
<div class='mdpoptag'>ABB</div>
<div class='mdpoptag'>AB</div>
Upvotes: 0