views:

45

answers:

2

Hi.

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

A: 

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>
artlung
+1  A: 

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");
}
Travis
If you use `text()`, you won't get any `<br>` tags.
bobince
My mistake. I've edited this to correct the error.
Travis