Reputation: 3587
I have an array of names, which i need to see if they match names in a DIV:
var topicName = [];
$.each(top3, function(i, item) {
if (item.featured === false) {
topicName.push(item.username);
}
});
$("DIV.grid_6 DIV.name").each(function(i, item){
if (topicName === item){
alert("do somethign");
}
}
I have a bunch of DIV.name classes and if the name matches the topicName array, i need to do something;
Upvotes: 2
Views: 420
Reputation: 25776
$("DIV.grid_6 DIV.name").each(function(i, item){
if ( $.inArray( item, topicName ) !== -1 ){
alert("do somethign");
}
}
Upvotes: 1
Reputation: 429
you can use some kind of tricky way like this;
$(document).ready(function(){
var a = ['foo', 'bar', 'c'];
//
// $('.' + a.join(', .')) => $('.foo, .bar, .c')
//
$('.' + a.join(', .')).mouseover(function() {
console.log($(this).attr('class'));
});
});
you can simply select them and do what ever you want. If the name is not exists, there will be no binding. It means no problem at all.
Upvotes: 0
Reputation: 14435
HTML:
<div class="name">jen</div>
<div class="name">bob</div>
<div class="name">sally</div>
jQuery:
var topicName = ["jen","bob","michelle"];
$("div.name").each(function(){
var nameFound = $.inArray($(this).text(), topicName);
if (nameFound === -1){
alert($(this).text() + " not found in array");
} else {
alert($(this).text() + " found in array");
}
});
Upvotes: 0
Reputation: 198388
Instead of
if (topicName === item)
do this, if the text to match is the content of the div
:
if ($.inArray($(item).text(), topicName) != -1)
or, if it's in an attribute (such as <div name="foo">
):
if ($.inArray($(item).attr('name'), topicName) != -1)
Upvotes: 0