Hide duplicate parameter value in select box using jquery or javascript
Using jquery / Js how to hide parameter value for repeated text.
Main Goal is Remove repeated text option value from select box.
I just want to hide Test1 and Test2 with identification values โโ4.5 and 6 because their value is repeated in the option field.
<select name="finish" id="finish">
<option value="">Finish</option>
<option id="1" value="1">Test1</option>
<option id="2" value="2">Test2</option>
<option id="3" value="3">Test3</option>
<option id="4" value="4">Test1</option>
<option id="5" value="5">Test1</option>
<option id="6" value="6">Test2</option>
</select>
jquery code tring below,
$(document).ready(function(){
$('#finish').each(function(){
//$(this).text().hide();
});
});
The final result looks like below,
<select name="finish" id="finish">
<option value="">Finish</option>
<option id="1" value="1">Test1</option>
<option id="2" value="2">Test2</option>
<option id="3" value="3">Test3</option>
<option id="4" value="4" style="display:none">Test1</option>
<option id="5" value="5" style="display:none">Test1</option>
<option id="6" value="6" style="display:none">Test2</option>
</select>
How to do it using javascript or jquery script.
Thank.
source to share
Initialize an empty array finishItems
.
Switch to selection options with
$("select > option").each(function(){
});
If this parameter is not listed, add it, if it exists removes it.
var finishItems = {};
$("select > option").each(function () {
if(finishItems[this.text]) {
$(this).remove();
} else {
finishItems[this.text] = this.value;
}});
$(document).ready(function(){
var finishItems = {};
$("select > option").each(function () {
if(finishItems[this.text]) {
$(this).remove();
} else {
finishItems[this.text] = this.value;
}});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="finish" id="finish">
<option value="">Finish</option>
<option id="1" value="1">Test1</option>
<option id="2" value="2">Test2</option>
<option id="3" value="3">Test3</option>
<option id="4" value="4">Test1</option>
<option id="5" value="5">Test1</option>
<option id="6" value="6">Test2</option>
</select>
source to share
You can use filter()
to filter items that have the same text as the previous item and one object to hold the text.
var obj = {}, select = $('select');
select.html(select.find('option').filter(function() {
var text = $(this).text();
return !obj[text] ? obj[text] = 1 : false;
}));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="finish" id="finish">
<option value="">Finish</option>
<option id="1" value="1">Test1</option>
<option id="2" value="2">Test2</option>
<option id="3" value="3">Test3</option>
<option id="4" value="4">Test1</option>
<option id="5" value="5">Test1</option>
<option id="6" value="6">Test2</option>
</select>
If you just want to add display: none
to duplicates, you can use a loop each
.
var obj = {},select = $('select');
select.find('option').each(function() {
var text = $(this).text();
!obj[text] ? obj[text] = 1 : $(this).css('display', 'none');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="finish" id="finish">
<option value="">Finish</option>
<option id="1" value="1">Test1</option>
<option id="2" value="2">Test2</option>
<option id="3" value="3">Test3</option>
<option id="4" value="4">Test1</option>
<option id="5" value="5">Test1</option>
<option id="6" value="6">Test2</option>
</select>
source to share
I think it works.
$(document).ready(function(){
var values231 = [];
$.each($('option'), function(){
var flag = false;
var thishtml = $(this).html();
for(var i = 0; i< values231.length; i++){
if(thishtml == values231[i]){
$(this).hide();
flag = true;
}
}
if(!flag){
values231.push(thishtml);
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<select name="finish" id="finish">
<option value="">Finish</option>
<option id="1" value="1">Test1</option>
<option id="2" value="2">Test2</option>
<option id="3" value="3">Test3</option>
<option id="4" value="4">Test1</option>
<option id="5" value="5">Test1</option>
<option id="6" value="6">Test2</option>
</select>
source to share
var arr = []
$("#finish option").each(function(i, v) {
if ($.inArray($(this).text(), arr) == -1) {
arr.push($(this).text())
} else {
$(this).prop("disabled", true)
}
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="finish" id="finish">
<option value="">Finish</option>
<option id="1" value="1">Test1</option>
<option id="2" value="2">Test2</option>
<option id="3" value="3">Test3</option>
<option id="4" value="4">Test1</option>
<option id="5" value="5">Test1</option>
<option id="6" value="6">Test2</option>
</select>
- Have an array where you can put the text of each option
- Check if there is text in the array, if so then disable
source to share
Try using the function Array#includes()
and Array#push
.Push of the array innerText
, and then check that the new element text is in the array. If push c is not present in array.else, hide the respectable element
$(document).ready(function() {
var arr = [];
$('#finish option').each(function() {
var val = $(this).text().trim();
if (!arr.includes(val)) {
arr.push(val)
} else {
$(this).hide();
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="finish" id="finish">
<option value="">Finish</option>
<option id="1" value="1">Test1</option>
<option id="2" value="2">Test2</option>
<option id="3" value="3">Test3</option>
<option id="4" value="4">Test1</option>
<option id="5" value="5">Test1</option>
<option id="6" value="6">Test2</option>
</select>
source to share
My approach is to create an object that contains the texts, and if the option text is already registered, it removes the parameter.
$(document).ready(function(){
var elements = {};
$('#finish option').each(function(){
console.log($(this).val(), this);
if(elements[$(this).text()]){
$(this).remove();
} else {
elements[$(this).text()] = true;
}
});
});
source to share
Simple (vanilla) Javascript solution:
var options = document.getElementById('finish').options;
var contents = [];
for (var i = 0; i < options.length; i++) {
if (contents.includes(options[i].textContent)) {
options[i].style.display = 'none';
} else {
contents.push(options[i].textContent);
}
}
In IE, hidden options don't work. In this case, the only reliable way is to remove the elements from the DOM and read them when needed.
source to share
Try something like this:
var arr = [];
$('#finish option').each(function(){
if( $.inArray($(this).text(), arr) == -1 )
{
arr.push($(this).text());
}
else
{
$(this).hide();
}
});
Explanation: Make an empty array in it and push a unique value in it, checking if it exists. And hide the options that are duplicated.
source to share
To provide only separate text:
var seen = {}
$(document).ready(function(){
$('#finish').each(function(){
if ( seen[ $(this).text() ] ) {
$(this).hide();
} else {
seen[$(this).text()] = true;
}
});
});
Part seen[ $(this).text() ]
undefined
if not set, so only the value of the invisible text will go to else
.
You can also set up a condition for more complex logic.
source to share
You can add data to parameters from an array. Find the unique one in this array.
Js
var array = ["Test1", "Test2", "Test3", "Test1", "Test1", "Test2"]
var json = _.uniq(array);
$.each(json, function(i, value) {
$('#finish').append($('<option>').text(value).attr({
'value': i+1,
'id': i+1
}));
});
Html
<select name="finish" id="finish" class="form-control">
<option value="">Finish</option>
</select>
source to share
Enable option and remove duplicate value.
var repeatText = {};
$("#finish > option").each(function () {
if(repeatText[this.text]) {
$(this).remove();
} else {
repeatText[this.text] = this.value;
}});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="finish" id="finish">
<option value="">Finish</option>
<option id="1" value="1">Test1</option>
<option id="2" value="2">Test2</option>
<option id="3" value="3">Test3</option>
<option id="4" value="4">Test1</option>
<option id="5" value="5">Test1</option>
<option id="6" value="6">Test2</option>
</select>
source to share
You can store the list text
in a new array (see arrHide
my code example), so in a loop using select options, you can check if the current text is in the array arrHide
, then hide the relative element.
See below:
var arrHide = [];
$("#finish > option").each(function() {
if($.inArray(this.text, arrHide) < 0){
arrHide.push(this.text);
}
else {
$(this).hide();
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="finish" id="finish">
<option value="">Finish</option>
<option id="1" value="1">Test1</option>
<option id="2" value="2">Test2</option>
<option id="3" value="3">Test3</option>
<option id="4" value="4">Test1</option>
<option id="5" value="5">Test1</option>
<option id="6" value="6">Test2</option>
</select>
Hope this helps you, bye.
source to share