I’m trying to clean up the results presented on my HTML file with Jquery. I want to keep removing words that are repeated more than one time.
A quick example
Accents Australian
Accents English (RP)
Dance Hip Hop
Dance Jazz
It should be output as
Accents
- Australian
- English (RP)
Dance
- Hip Hop
- Jazz
My original HTML looks like this
<div role="list" class="skill-items">
<div role="listitem" class="skill-item">
<div class="skill-category">Accents</div>
<div>Australian</div>
</div>
<div role="listitem" class="skill-item">
<div class="skill-category">Accents</div>
<div>English (RP)</div>
</div>
<div role="listitem" class="skill-item">
<div class="skill-category">Dance</div>
<div>Hip Hop</div>
</div>
<div role="listitem" class="skill-item">
<div class="skill-category">Dance</div>
<div>Jaz</div>
</div>
</div>
I tried my best but I’m not landing in a good place
$('.skill-category').text(function(index, oldText) {
return oldText.replace($(this).parent().next().find('.skill-category').text(), '');
})
Any suggestion?
2
Answers
Please check below working code:
As per you question and shared HTML above is the working code for the same and if you add more similar things it will help.
Please let me know if you find any issues
Your question can be broken into two problems:
.skill-category
<div>
elements into a list.Grouping the elements could by done like so:
.skill-category
(in your example HTML, that’s a single<div>
. Cut-and-paste it at the end of the aforementioned previous element.For the second problem:
Changing an element (
<div>
to<li>
) is not possible. You can create a new<li>
and move what’s inside the<div>
into it. Of course, you’ll need a<ul>
that wraps the<li>s
as well..skill-category
elements<div>
elements)<li>
.<li>s
of a single category into a<ul>
.<div>(s)
) since we’ve moved all their content to a different node. They’re now empty tags and useless.<ul>
after the.skill-category
.