Dom
Dom

Reputation: 3126

Remove same replicated text with jQuery

This might be a little bit odd question, but Im trying to figure out function that will remove numbers that are repeating them self.

this is my html

1234123<br>
23434<br>
5696<br>
5696<br>
34096756098<br>

I need function that will return numbers numbers without those that are duplicate them self

1234123<br>
23434<br>
5696<br>
34096756098<br>

Thank you for your help in advance

Upvotes: 1

Views: 100

Answers (2)

Travis
Travis

Reputation: 4078

You basically need to get your string, via str = $('#myelement').html(), then run the function below, which splits the string by line break, strips non-unique elements, then joins it together again:

function uniquediv(str) {
    this.input = str.split("<br>\n");
    this.output = [];
    this.output.contains = function (value) {
        for (this.j in this) {
            if (this[this.j] == value)
                return true;
        }
        return false;
    }
    for (this.i in this.input) {
        if (!this.output.contains(this.input[i]))
            this.output.push(this.input[i]);
    }
    return this.output.join("<br>\n");
}

Upvotes: 1

artlung
artlung

Reputation: 34013

How about this: split the content by <br>, then interate over that into a new temporary array, then replace it in place:

var content = $('#demo').html().split('<br>');
var tempArray = [];
for (var i=0; i<content.length; i++) {
    if ($.inArray(content[i], tempArray) === -1) {
        tempArray.push(content[i]);
    }
}
$('#demo').html(tempArray.join('<br>'));

for this html:

<div id="demo">
    1234123<br>
    23434<br>
    5696<br>
    5696<br>
    34096756098<br>
</div>

Upvotes: 0

Related Questions