I'm trying to remove duplicate lines in a div
<div id="content>
<p>Hello</p>
<p>why</p>
<p>are</p>
<p>hello</p>
</div>
The output is shown as
hello
why
are
hello
How can I remove the duplicate lines?
I'm trying to remove duplicate lines in a div
<div id="content>
<p>Hello</p>
<p>why</p>
<p>are</p>
<p>hello</p>
</div>
The output is shown as
hello
why
are
hello
How can I remove the duplicate lines?
If you are concerned about case, just remove the .toLowerCase()
var seen = {};
$('#content>p').each(function() {
var txt = $(this).text().toLowerCase();
if (seen[txt])
$(this).remove();
else
seen[txt] = true;
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<div id="content">
<p>Hello</p>
<p>why</p>
<p>are</p>
<p>hello</p>
</div>
use an object as map
var map = {};
$('p').each(function(){
var value = $(this).text();
if (map[value] == null){
map[value] = true;
} else {
$(this).remove();
}
});
Duplicate lines? Could you please post a screenshot of how it looks? Do you want the wording spaced closer to each other in the paragraph tags? If this is the case, try using CSS
#content {
line-height: 0.5;
}
Also, you missed a " in id="content"