user994461
user994461

Reputation: 530

Display same class only once with jquery

How i can make in this JS to display only once same class?

In the example below you can see 3 times class="33" but i need to display only once div with same class...

JS

function callMeOften()
{
     $.ajax({
        method: 'get',
        url : '1.php',
        dataType : 'text',
        success: function (text) {
            $('#updateMe').html(text)  ;
        }
    });
}
var holdTheInterval = setInterval(callMeOften, 500000);
$(".<? echo $userRank['userID'] ?>").appendTo("#statMSG1");
}); 

html

<div id="33" class="33">Test</div>
<div id="33" class="33">Test</div>
<div id="45" class="45">Test2</div>
<div id="33" class="33">Test</div>

Upvotes: 0

Views: 273

Answers (1)

Jasper
Jasper

Reputation: 76003

When you add your server-response to the DOM you can just select the first element with the 33 class and only add it:

$("#updateMe").html($(text).filter(".33").eq(0));​

Here is a demo: http://jsfiddle.net/BsVFj/

Notice the use of .filter({selector}) to return only element(s) in the current set that match the selector. Since your elements are siblings that have no parent you have to use .filter() to narrow then down, .find() only looks in descendant elements.

Some documentation for ya:

Upvotes: 1

Related Questions