how to check if a div with a certain value is duplicated and then hide the duplicated div in jQuery
up vote
0
down vote
favorite
I am having trouble checking if a div with a certain value is duplicated and then hiding that duplicated div.
Here's my code:
<div class="div">Java</div>
<div class="div">Python</div>
<div class="div">Php</div>
<div class="div">Java</div>
<div class="div">Java</div>
<div class="div">Java</div>
javascript jquery html
add a comment |
up vote
0
down vote
favorite
I am having trouble checking if a div with a certain value is duplicated and then hiding that duplicated div.
Here's my code:
<div class="div">Java</div>
<div class="div">Python</div>
<div class="div">Php</div>
<div class="div">Java</div>
<div class="div">Java</div>
<div class="div">Java</div>
javascript jquery html
What javascript have you tried?
– Nick Parsons
Nov 10 at 5:45
@NickParsons Nothing yet because I don't know where to start
– Ajay Nath
Nov 10 at 5:53
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am having trouble checking if a div with a certain value is duplicated and then hiding that duplicated div.
Here's my code:
<div class="div">Java</div>
<div class="div">Python</div>
<div class="div">Php</div>
<div class="div">Java</div>
<div class="div">Java</div>
<div class="div">Java</div>
javascript jquery html
I am having trouble checking if a div with a certain value is duplicated and then hiding that duplicated div.
Here's my code:
<div class="div">Java</div>
<div class="div">Python</div>
<div class="div">Php</div>
<div class="div">Java</div>
<div class="div">Java</div>
<div class="div">Java</div>
<div class="div">Java</div>
<div class="div">Python</div>
<div class="div">Php</div>
<div class="div">Java</div>
<div class="div">Java</div>
<div class="div">Java</div>
<div class="div">Java</div>
<div class="div">Python</div>
<div class="div">Php</div>
<div class="div">Java</div>
<div class="div">Java</div>
<div class="div">Java</div>
javascript jquery html
javascript jquery html
edited Nov 10 at 5:45
Nick Parsons
3,0952620
3,0952620
asked Nov 10 at 5:44
Ajay Nath
31
31
What javascript have you tried?
– Nick Parsons
Nov 10 at 5:45
@NickParsons Nothing yet because I don't know where to start
– Ajay Nath
Nov 10 at 5:53
add a comment |
What javascript have you tried?
– Nick Parsons
Nov 10 at 5:45
@NickParsons Nothing yet because I don't know where to start
– Ajay Nath
Nov 10 at 5:53
What javascript have you tried?
– Nick Parsons
Nov 10 at 5:45
What javascript have you tried?
– Nick Parsons
Nov 10 at 5:45
@NickParsons Nothing yet because I don't know where to start
– Ajay Nath
Nov 10 at 5:53
@NickParsons Nothing yet because I don't know where to start
– Ajay Nath
Nov 10 at 5:53
add a comment |
4 Answers
4
active
oldest
votes
up vote
0
down vote
accepted
Try this,I hope this is what you are looking for.
var a = new Array();
$('.div').each(function(index)
text = $(this).text();
if($.inArray(text, a)!=-1)
$(this).closest('.div').hide();
else
a.push(text);
);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="div">Java</div>
<div class="div">Python</div>
<div class="div">Php</div>
<div class="div">Java</div>
<div class="div">Java</div>
<div class="div">Java</div>
thanks so much this has fixed my problem :)
– Ajay Nath
Nov 10 at 7:25
ok..Happy to help you..All the best :)
– Ashwin
Nov 10 at 9:03
add a comment |
up vote
0
down vote
You need to loop through all div classes and then check if the current div class is already exist, this will check using div class html content. Script is like this:
var divs = ;
$(".div").each(function(i,v)
if(divs.indexOf(v.html())>=0)
divs.push(v.html()); //collect all unique divs
else
//this is the duplicate div. need to hide it.
$(this).hide();
);
add a comment |
up vote
0
down vote
well, it depends, if you want to check if hes inside text is the same then you can run over all the divs and use something like array to check if already exists.
var exists = ;
$(document).ready(function()
$(".div").each(function()
var text = $(this).text();
if(exists.indexOf(text) != -1)
$(this).hide();
else
exists.push(text);
)
)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="div">Java</div>
<div class="div">Python</div>
<div class="div">Php</div>
<div class="div">Java</div>
<div class="div">Java</div>
<div class="div">Java</div>
add a comment |
up vote
0
down vote
You can wrap all the elements you want to remove the duplicates in a class .container
You can then get all the children of this element, and turn it into an array using .toArray()
. You can then use a Set
to remove all the duplicates in this array:
and then use a Set
to remove the duplicates within the array and then display this:
const unique = $('.container').children().toArray().map(e => '' + e.outerHTML);
const elems = [...new Set(unique)].join('');
$('.container').html(elems);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
<div class="div">Java</div>
<br />
<div class="div">Python</div>
<div class="div">Php</div>
<div class="div">Java</div>
<div class="div">Java</div>
<div class="div">Java</div>
</div>
If you wish you could use this as a one-liner:
$('.container').html([...new Set($('.container').children().toArray().map(e => ''+e.outerHTML))].join(''));
add a comment |
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
Try this,I hope this is what you are looking for.
var a = new Array();
$('.div').each(function(index)
text = $(this).text();
if($.inArray(text, a)!=-1)
$(this).closest('.div').hide();
else
a.push(text);
);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="div">Java</div>
<div class="div">Python</div>
<div class="div">Php</div>
<div class="div">Java</div>
<div class="div">Java</div>
<div class="div">Java</div>
thanks so much this has fixed my problem :)
– Ajay Nath
Nov 10 at 7:25
ok..Happy to help you..All the best :)
– Ashwin
Nov 10 at 9:03
add a comment |
up vote
0
down vote
accepted
Try this,I hope this is what you are looking for.
var a = new Array();
$('.div').each(function(index)
text = $(this).text();
if($.inArray(text, a)!=-1)
$(this).closest('.div').hide();
else
a.push(text);
);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="div">Java</div>
<div class="div">Python</div>
<div class="div">Php</div>
<div class="div">Java</div>
<div class="div">Java</div>
<div class="div">Java</div>
thanks so much this has fixed my problem :)
– Ajay Nath
Nov 10 at 7:25
ok..Happy to help you..All the best :)
– Ashwin
Nov 10 at 9:03
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
Try this,I hope this is what you are looking for.
var a = new Array();
$('.div').each(function(index)
text = $(this).text();
if($.inArray(text, a)!=-1)
$(this).closest('.div').hide();
else
a.push(text);
);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="div">Java</div>
<div class="div">Python</div>
<div class="div">Php</div>
<div class="div">Java</div>
<div class="div">Java</div>
<div class="div">Java</div>
Try this,I hope this is what you are looking for.
var a = new Array();
$('.div').each(function(index)
text = $(this).text();
if($.inArray(text, a)!=-1)
$(this).closest('.div').hide();
else
a.push(text);
);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="div">Java</div>
<div class="div">Python</div>
<div class="div">Php</div>
<div class="div">Java</div>
<div class="div">Java</div>
<div class="div">Java</div>
var a = new Array();
$('.div').each(function(index)
text = $(this).text();
if($.inArray(text, a)!=-1)
$(this).closest('.div').hide();
else
a.push(text);
);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="div">Java</div>
<div class="div">Python</div>
<div class="div">Php</div>
<div class="div">Java</div>
<div class="div">Java</div>
<div class="div">Java</div>
var a = new Array();
$('.div').each(function(index)
text = $(this).text();
if($.inArray(text, a)!=-1)
$(this).closest('.div').hide();
else
a.push(text);
);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="div">Java</div>
<div class="div">Python</div>
<div class="div">Php</div>
<div class="div">Java</div>
<div class="div">Java</div>
<div class="div">Java</div>
answered Nov 10 at 5:58
Ashwin
1366
1366
thanks so much this has fixed my problem :)
– Ajay Nath
Nov 10 at 7:25
ok..Happy to help you..All the best :)
– Ashwin
Nov 10 at 9:03
add a comment |
thanks so much this has fixed my problem :)
– Ajay Nath
Nov 10 at 7:25
ok..Happy to help you..All the best :)
– Ashwin
Nov 10 at 9:03
thanks so much this has fixed my problem :)
– Ajay Nath
Nov 10 at 7:25
thanks so much this has fixed my problem :)
– Ajay Nath
Nov 10 at 7:25
ok..Happy to help you..All the best :)
– Ashwin
Nov 10 at 9:03
ok..Happy to help you..All the best :)
– Ashwin
Nov 10 at 9:03
add a comment |
up vote
0
down vote
You need to loop through all div classes and then check if the current div class is already exist, this will check using div class html content. Script is like this:
var divs = ;
$(".div").each(function(i,v)
if(divs.indexOf(v.html())>=0)
divs.push(v.html()); //collect all unique divs
else
//this is the duplicate div. need to hide it.
$(this).hide();
);
add a comment |
up vote
0
down vote
You need to loop through all div classes and then check if the current div class is already exist, this will check using div class html content. Script is like this:
var divs = ;
$(".div").each(function(i,v)
if(divs.indexOf(v.html())>=0)
divs.push(v.html()); //collect all unique divs
else
//this is the duplicate div. need to hide it.
$(this).hide();
);
add a comment |
up vote
0
down vote
up vote
0
down vote
You need to loop through all div classes and then check if the current div class is already exist, this will check using div class html content. Script is like this:
var divs = ;
$(".div").each(function(i,v)
if(divs.indexOf(v.html())>=0)
divs.push(v.html()); //collect all unique divs
else
//this is the duplicate div. need to hide it.
$(this).hide();
);
You need to loop through all div classes and then check if the current div class is already exist, this will check using div class html content. Script is like this:
var divs = ;
$(".div").each(function(i,v)
if(divs.indexOf(v.html())>=0)
divs.push(v.html()); //collect all unique divs
else
//this is the duplicate div. need to hide it.
$(this).hide();
);
answered Nov 10 at 5:56
McBern
11615
11615
add a comment |
add a comment |
up vote
0
down vote
well, it depends, if you want to check if hes inside text is the same then you can run over all the divs and use something like array to check if already exists.
var exists = ;
$(document).ready(function()
$(".div").each(function()
var text = $(this).text();
if(exists.indexOf(text) != -1)
$(this).hide();
else
exists.push(text);
)
)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="div">Java</div>
<div class="div">Python</div>
<div class="div">Php</div>
<div class="div">Java</div>
<div class="div">Java</div>
<div class="div">Java</div>
add a comment |
up vote
0
down vote
well, it depends, if you want to check if hes inside text is the same then you can run over all the divs and use something like array to check if already exists.
var exists = ;
$(document).ready(function()
$(".div").each(function()
var text = $(this).text();
if(exists.indexOf(text) != -1)
$(this).hide();
else
exists.push(text);
)
)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="div">Java</div>
<div class="div">Python</div>
<div class="div">Php</div>
<div class="div">Java</div>
<div class="div">Java</div>
<div class="div">Java</div>
add a comment |
up vote
0
down vote
up vote
0
down vote
well, it depends, if you want to check if hes inside text is the same then you can run over all the divs and use something like array to check if already exists.
var exists = ;
$(document).ready(function()
$(".div").each(function()
var text = $(this).text();
if(exists.indexOf(text) != -1)
$(this).hide();
else
exists.push(text);
)
)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="div">Java</div>
<div class="div">Python</div>
<div class="div">Php</div>
<div class="div">Java</div>
<div class="div">Java</div>
<div class="div">Java</div>
well, it depends, if you want to check if hes inside text is the same then you can run over all the divs and use something like array to check if already exists.
var exists = ;
$(document).ready(function()
$(".div").each(function()
var text = $(this).text();
if(exists.indexOf(text) != -1)
$(this).hide();
else
exists.push(text);
)
)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="div">Java</div>
<div class="div">Python</div>
<div class="div">Php</div>
<div class="div">Java</div>
<div class="div">Java</div>
<div class="div">Java</div>
var exists = ;
$(document).ready(function()
$(".div").each(function()
var text = $(this).text();
if(exists.indexOf(text) != -1)
$(this).hide();
else
exists.push(text);
)
)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="div">Java</div>
<div class="div">Python</div>
<div class="div">Php</div>
<div class="div">Java</div>
<div class="div">Java</div>
<div class="div">Java</div>
var exists = ;
$(document).ready(function()
$(".div").each(function()
var text = $(this).text();
if(exists.indexOf(text) != -1)
$(this).hide();
else
exists.push(text);
)
)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="div">Java</div>
<div class="div">Python</div>
<div class="div">Php</div>
<div class="div">Java</div>
<div class="div">Java</div>
<div class="div">Java</div>
answered Nov 10 at 5:59
Talg123
21118
21118
add a comment |
add a comment |
up vote
0
down vote
You can wrap all the elements you want to remove the duplicates in a class .container
You can then get all the children of this element, and turn it into an array using .toArray()
. You can then use a Set
to remove all the duplicates in this array:
and then use a Set
to remove the duplicates within the array and then display this:
const unique = $('.container').children().toArray().map(e => '' + e.outerHTML);
const elems = [...new Set(unique)].join('');
$('.container').html(elems);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
<div class="div">Java</div>
<br />
<div class="div">Python</div>
<div class="div">Php</div>
<div class="div">Java</div>
<div class="div">Java</div>
<div class="div">Java</div>
</div>
If you wish you could use this as a one-liner:
$('.container').html([...new Set($('.container').children().toArray().map(e => ''+e.outerHTML))].join(''));
add a comment |
up vote
0
down vote
You can wrap all the elements you want to remove the duplicates in a class .container
You can then get all the children of this element, and turn it into an array using .toArray()
. You can then use a Set
to remove all the duplicates in this array:
and then use a Set
to remove the duplicates within the array and then display this:
const unique = $('.container').children().toArray().map(e => '' + e.outerHTML);
const elems = [...new Set(unique)].join('');
$('.container').html(elems);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
<div class="div">Java</div>
<br />
<div class="div">Python</div>
<div class="div">Php</div>
<div class="div">Java</div>
<div class="div">Java</div>
<div class="div">Java</div>
</div>
If you wish you could use this as a one-liner:
$('.container').html([...new Set($('.container').children().toArray().map(e => ''+e.outerHTML))].join(''));
add a comment |
up vote
0
down vote
up vote
0
down vote
You can wrap all the elements you want to remove the duplicates in a class .container
You can then get all the children of this element, and turn it into an array using .toArray()
. You can then use a Set
to remove all the duplicates in this array:
and then use a Set
to remove the duplicates within the array and then display this:
const unique = $('.container').children().toArray().map(e => '' + e.outerHTML);
const elems = [...new Set(unique)].join('');
$('.container').html(elems);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
<div class="div">Java</div>
<br />
<div class="div">Python</div>
<div class="div">Php</div>
<div class="div">Java</div>
<div class="div">Java</div>
<div class="div">Java</div>
</div>
If you wish you could use this as a one-liner:
$('.container').html([...new Set($('.container').children().toArray().map(e => ''+e.outerHTML))].join(''));
You can wrap all the elements you want to remove the duplicates in a class .container
You can then get all the children of this element, and turn it into an array using .toArray()
. You can then use a Set
to remove all the duplicates in this array:
and then use a Set
to remove the duplicates within the array and then display this:
const unique = $('.container').children().toArray().map(e => '' + e.outerHTML);
const elems = [...new Set(unique)].join('');
$('.container').html(elems);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
<div class="div">Java</div>
<br />
<div class="div">Python</div>
<div class="div">Php</div>
<div class="div">Java</div>
<div class="div">Java</div>
<div class="div">Java</div>
</div>
If you wish you could use this as a one-liner:
$('.container').html([...new Set($('.container').children().toArray().map(e => ''+e.outerHTML))].join(''));
const unique = $('.container').children().toArray().map(e => '' + e.outerHTML);
const elems = [...new Set(unique)].join('');
$('.container').html(elems);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
<div class="div">Java</div>
<br />
<div class="div">Python</div>
<div class="div">Php</div>
<div class="div">Java</div>
<div class="div">Java</div>
<div class="div">Java</div>
</div>
const unique = $('.container').children().toArray().map(e => '' + e.outerHTML);
const elems = [...new Set(unique)].join('');
$('.container').html(elems);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
<div class="div">Java</div>
<br />
<div class="div">Python</div>
<div class="div">Php</div>
<div class="div">Java</div>
<div class="div">Java</div>
<div class="div">Java</div>
</div>
edited Nov 10 at 6:11
answered Nov 10 at 6:05
Nick Parsons
3,0952620
3,0952620
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53236322%2fhow-to-check-if-a-div-with-a-certain-value-is-duplicated-and-then-hide-the-dupli%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What javascript have you tried?
– Nick Parsons
Nov 10 at 5:45
@NickParsons Nothing yet because I don't know where to start
– Ajay Nath
Nov 10 at 5:53