0

I-m trying to copy the value from one select to another. One has the value 'A' and the other has the value 'B'. Here's what I'm doing to achieve this:

$('#selectTipoInvervencion1').val($('#selectTipoIntervencion0').val());

However, my value is not getting copied. The weird thing is, I'm getting the elements correctly since after executing this line of code I can do:

console.log($('#selectTipoIntervencion0').val());
console.log($('#selectTipoIntervencion1').val());

and the output is:

A
B

which is what I expected since the value doesn't appear to be copied in the view but is obviously not what I want. How can I fix this?

Update: HTML

<select id="selectTipoIntervencion0" name="forms[0].model.intervenciones[0].tipo" class="selectTiposIntervencion">
    <option name="A_NAME" value="A" selected="selected">A</option>
    <option name="B_NAME" value="B">B</option>
    ...
</select>

<select id="selectTipoIntervencion1" name="forms[1].model.intervenciones[1].tipo" class="selectTiposIntervencion">
    <option name="A_NAME" value="A" selected="selected">A</option>
    <option name="B_NAME" value="B">B</option>
    ...
</select>
David Antelo
  • 503
  • 6
  • 19

2 Answers2

0

Try this,

$('#selectTipoInvervencion0 option').clone().appendTo('#selectTipoIntervencion1');

Javascript: How to copy all options from one select element to another?

    function copyOption() {
       $('#selectTipoIntervencion1 option').clone().appendTo('#selectTipoIntervencion0');
     }
    
    
    
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <select id="selectTipoIntervencion0" name="forms[0].model.intervenciones[0].tipo" class="selectTiposIntervencion">
        <option name="A_NAME" value="A" selected="selected">A</option>
        <option name="B_NAME" value="B">B</option>          
     </select>
            
   <select id="selectTipoIntervencion1" name="forms[1].model.intervenciones[1].tipo" class="selectTiposIntervencion">
        <option name="A_NAME" value="A" selected="selected">A1</option>
        <option name="B_NAME" value="B">B2</option>
   </select>
            <button onclick="copyOption()">Copy</button>
Jeni
  • 320
  • 2
  • 12
  • Btw, funny thing is, my whole form is already a copy of the last one. I create this new form by cloning the last one. All values are copied correctly except for the selects – David Antelo Mar 16 '20 at 10:35
  • you mean select values are not cloning correctly, please check here https://stackoverflow.com/questions/742810/clone-isnt-cloning-select-values – Jeni Mar 16 '20 at 10:42
0

Try this

<!DOCTYPE html>
<html>
    <head>
        <title></title>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
    </head>
    <body>
        <select id="selectTipoIntervencion0" name="forms[0].model.intervenciones[0].tipo" class="selectTiposIntervencion">
            <option name="B_NAME" value="B">B</option>
            <option name="A_NAME" value="A">A</option>
        </select>

        <select id="selectTipoIntervencion1" name="forms[1].model.intervenciones[1].tipo" class="selectTiposIntervencion">
            <option name="A_NAME" value="A">A</option>
            <option name="B_NAME" value="B">B</option>
            <option name="C_NAME" value="C">C</option>
            <option name="D_NAME" value="D">D</option>
            <option name="E_NAME" value="E" selected="selected">E</option>
            <option name="F_NAME" value="F">F</option>
            <option name="G_NAME" value="G">G</option>
            <option name="H_NAME" value="H">H</option>
        </select>
        <button type="button" id="cp">Copy</button>

        <script type="text/javascript">
            $("#cp").on('click', (evt) => {
                let a = $("#selectTipoIntervencion1").children("option:selected");
                $("#selectTipoIntervencion0").html(a);
            })
        </script>
    </body>
</html>
Pushpendra Kumar
  • 1,721
  • 1
  • 15
  • 21