Xtian Xtian - 2 months ago 7
jQuery Question

match array with text in DIV

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;

jk. jk.
Answer

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");
    }

 });

http://jsfiddle.net/xqGKr/