Copy select and keep selectedIndex

0

I'm messing with a Select, where I hedge its index. The problem that after setting the index I have to put it inside a div.

But it does not get the index set, it goes with the default. What function of jquery, cuts the select with what was assigned to it, so I can do the insertion of what I want.

<select class="my">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
</select>

<select class="menor">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
</select>

$(document).ready(function() {
        $(".my, .menor").prop('selectedIndex', -1);     
        $(".my").after("<div class='maior'></div>").prependTo(".maior");
});

Ex: link

    
asked by anonymous 15.05.2015 / 23:04

2 answers

1

You can use this to copy the index from one select to another:

var index = $(this).prop('selectedIndex'); // $(this) ou um seletor que dê o select original
$(".menor").prop('selectedIndex', index);  // $(".menor") ou um selector que dê o select de destino

An example: link

To insert .menor into a new div you can use .wrap() fault% of jQuery with

$(".my").wrap("<div class='maior'></div>");

An example of the two working together: link

    
15.05.2015 / 23:12
0

I was able to do this:

Instead of just using prependTo, I've cloned:

clone().prependTo()...

$(html).find("select").prop('selectedIndex', $(this).find(":selected").index());
$(this).remove();
    
15.05.2015 / 23:47