JavaScript To avoid duplicate drop-down items with JQuery (For Create Compare)

Excuse me (I can not write good English!) i want to when change combobox selected item, this item (selected item) hide in other combobox and when change again selected hide item show again in other combobox.

<select class="soma1">
    <option>item1</option>
    <option>item2</option>
    <option>item3</option>
    <option>item4</option>
</select>

<select class="soma2">
    <option>item1</option>
    <option>item2</option>
    <option>item3</option>
    <option>item4</option>
</select>

<script type="text/javascript">
$('.soma1').change(function () {
    var cm = $('.soma1').text();
    $('.soma2 option:contains(' + cm + ')').each(function () {
        if ($(this).text() == cm) {
            $(this).remove();
        }
    });
});
</script>
Answer:1

You could also use $('.soma1').find('option:selected').html();

Here is a fiddle

<select class="soma1">
    <option>item1</option>
    <option>item2</option>
    <option>item3</option>
    <option>item4</option>
</select>

<select class="soma2">
   <option>item1</option>
   <option>item2</option>
   <option>item3</option>
   <option>item4</option>
</select>

<script type="text/javascript">
$('.soma1').change(function () {
    var selectedItem = $('.soma1').find('option:selected');
    $('.soma2').find('option').each(function(){
        if (selectedItem.html() != $(this).html()){
            $(this).remove();
        }
    });
});
</script>
Answer:2

Instead of

var cm = $('.soma1').text();

use

var cm = $('.soma1').val();
Answer:3

TreeView creation: function CreateNotificationTree(userId) { debugger; var data = new kendo.data.HierarchicalDataSource({ transport: { read: { url: "../api/...

TreeView creation: function CreateNotificationTree(userId) { debugger; var data = new kendo.data.HierarchicalDataSource({ transport: { read: { url: "../api/...

I created a small api to generate test data on the fly. Each call creates a fresh user and returns the created data. To load the data I use the package request: var flow = protractor.promise....

I created a small api to generate test data on the fly. Each call creates a fresh user and returns the created data. To load the data I use the package request: var flow = protractor.promise....

I want to display a jQuery UI tabs inside a Dialog but it shows up Directory Listing of my asp.net app for no reason. It also changes the css of my page. What's going on here? Javascript: $("#...

I want to display a jQuery UI tabs inside a Dialog but it shows up Directory Listing of my asp.net app for no reason. It also changes the css of my page. What's going on here? Javascript: $("#...

I am trying to get some event listeners to work, but for some reason they are not working properly. The two event blocks below work fine on their own, but together only the removes block works. If I ...

I am trying to get some event listeners to work, but for some reason they are not working properly. The two event blocks below work fine on their own, but together only the removes block works. If I ...

  1. getelementsbyclassname doesn't work
  2. getelementsbyclassname doesn't work in ie
  3. document.getelementsbyclassname doesn't work
  4. javascript getelementsbyclassname doesn't work
  5. getelementsbyclassname object doesn't support this property