Unable to append and update option from select selected jquery

Mayren Martinez

I'm trying to append one option selected to another select. The issue i'm having is that i'm able to append one of the option as desire however if i pick a different option within the same select, the option gets appended as well which not what i want. I want to update the appended options if a different one is selected. So far this is what i have. Select is dynamic ("next_exp") is the id of each select created.

 $('#myselect'+next_exp).on( 'change', function() {
    var sel = $('[id^="select-reflecting-option-selected"]'), opt = $( '<option/>' );
   if (sel.find('option').text().indexOf(this.id) > 0) {
        $('option[value=' + this.id + ']').remove();
          $("select[select-reflecting-option-selected] option:selected").remove();
    } else {
       sel.append( $("<option />").text(this.value).val( this.value));
    }

});
Louys Patrice Bessette

I think you made something simple looking complex.

All you have to do is find the .selectedIndex and use it to "move" the option the the other select using .eq() and .append().

You don't have to remove it and create a new... When you append an exiting element somewhere else, that is a "move"... Not a .clone().

$('#myselect').on( 'change', function() {
  var selectedIndex = $(this)[0].selectedIndex
  if( selectedIndex > 0){
    $("#myotherselect").append( $(this).find("option").eq(selectedIndex) );
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<select id="myselect">
  <option>1</option>
  <option>2</option>
  <option>3</option>
  <option>4</option>
  <option>5</option>
</select>

<select id="myotherselect">
  <option>A</option>
  <option>B</option>
  <option>C</option>
  <option>D</option>
  <option>E</option>
</select>


EDIT

I didn't get that aspect of checking if the option is already in the second select...

This need an additional verification, obviously.
Look the comments in code.

$('#myselect').on( 'change', function() {

  // Selected index from 1st select element.
  var selectedIndex = $(this)[0].selectedIndex;
  // The text of the selected option (will be used to compare).
  var selectedText = $(this).find("option").eq(selectedIndex).text();

  // A collection of all the option elements from the other select.
  var otherSelectOptions = $("#myotherselect").find("option");

  // An empty array to store the text of all options in the other select.
  var otherSelectOptionTexts = [];

  // Loop to fill the array above.
  for(i=0;i<otherSelectOptions.length;i++){
    otherSelectOptionTexts.push(otherSelectOptions.eq(i).text());
  }

  // Some console logs to know what happens here...
  console.log(JSON.stringify(otherSelectOptionTexts));
  console.log(selectedText);

  // If selected option is not the first.
  if( selectedIndex > 0){

    // If the selected option's text isn't found in the array filled previously.
    if(otherSelectOptionTexts.indexOf(selectedText) == -1){
      console.log("Option moved to the other select.");

      // Move the option in the other select
      $("#myotherselect").append( $(this).find("option").eq(selectedIndex) );

      // If the text is found.
    }else{
      console.log("Option was already in the other select... Just removed.");

      // Just remove the option
      $(this).find("option").eq(selectedIndex).remove();
    }
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<select id="myselect">
  <option>--Select a number</option>
  <option>1</option>
  <option>2</option>
  <option>3</option>
  <option>4</option>
  <option>5</option>
</select>

<select id="myotherselect">
  <option>--Select a letter/number</option>
  <option>A</option>
  <option>B</option>
  <option>3</option>
  <option>C</option>
  <option>D</option>
  <option>E</option>
</select>

The number 3 from the first select will not be "moved"... Just removed, because there is already an option 3 in the second select.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Update select options based on other selected option from database

From Dev

Grab id from a select option field and update it in the selected user

From Dev

How to dynamically append selected option to a div and update if selected option is changed?

From Dev

How to get attribute from selected option of select in jQuery?

From Dev

jQuery Multi Select Option - Check if a option is selected or not

From Dev

Using .append() in a loop to add text from an object after a <select> based on the selected option

From Dev

Option selected != .val from jquery

From Java

jquery select change event get selected option

From Dev

JQuery Copy Selected Select option to input

From Java

How to make the first option of <select> selected with jQuery

From Dev

JQuery (Mobile) / JavaScript Select option cannot be selected

From Dev

jQuery adding option to select box with selected arbitrate

From Dev

jQuery .clone() of a <select>: change the selected option

From Dev

Remove last selected element from the multiple select if user select more than 3 option using jquery

From Dev

Append selected option to FormData

From Dev

Append <option> and Select Value From DB

From Dev

Select option selected an not selected

From Dev

Render select Tag from jQuery Ajax , then get the value from selected option

From Dev

Remove selected option from another select box

From Dev

Need to get selected option from Select component

From Dev

Get selected option value from specific select

From Dev

Need to get selected option from Select component

From Dev

Append to an object selected from a list in jQuery

From Dev

Update select react from another select option

From Dev

Unable to set select selected value using jquery

From Dev

Unable to set select selected value using jquery

From Dev

How can I exit from a jQuery event when an "unrelated" html select element has had no option selected?

From Dev

Set class from <select> if selected option contains ID='answer' in HTML/JavaScript (JQuery)

From Dev

How do I fire a jQuery with an option selected from a Select2 dropdown?

Related Related

  1. 1

    Update select options based on other selected option from database

  2. 2

    Grab id from a select option field and update it in the selected user

  3. 3

    How to dynamically append selected option to a div and update if selected option is changed?

  4. 4

    How to get attribute from selected option of select in jQuery?

  5. 5

    jQuery Multi Select Option - Check if a option is selected or not

  6. 6

    Using .append() in a loop to add text from an object after a <select> based on the selected option

  7. 7

    Option selected != .val from jquery

  8. 8

    jquery select change event get selected option

  9. 9

    JQuery Copy Selected Select option to input

  10. 10

    How to make the first option of <select> selected with jQuery

  11. 11

    JQuery (Mobile) / JavaScript Select option cannot be selected

  12. 12

    jQuery adding option to select box with selected arbitrate

  13. 13

    jQuery .clone() of a <select>: change the selected option

  14. 14

    Remove last selected element from the multiple select if user select more than 3 option using jquery

  15. 15

    Append selected option to FormData

  16. 16

    Append <option> and Select Value From DB

  17. 17

    Select option selected an not selected

  18. 18

    Render select Tag from jQuery Ajax , then get the value from selected option

  19. 19

    Remove selected option from another select box

  20. 20

    Need to get selected option from Select component

  21. 21

    Get selected option value from specific select

  22. 22

    Need to get selected option from Select component

  23. 23

    Append to an object selected from a list in jQuery

  24. 24

    Update select react from another select option

  25. 25

    Unable to set select selected value using jquery

  26. 26

    Unable to set select selected value using jquery

  27. 27

    How can I exit from a jQuery event when an "unrelated" html select element has had no option selected?

  28. 28

    Set class from <select> if selected option contains ID='answer' in HTML/JavaScript (JQuery)

  29. 29

    How do I fire a jQuery with an option selected from a Select2 dropdown?

HotTag

Archive