Jitender
Jitender

Reputation: 7969

how to unwrap only one parent in jquery

I have optgroup in select menu, I want get rid of from it. I am using jquery unwrap method to get it done but it is removing whole select menu. here is fiddle

$(function(){
   $('select').find('option').not(':first').parent().unwrap()
})

HTML

<select>
    <option>select</option>
    <optgroup label="state">
        <option>new york</option>        
    </optgroup>
    <optgroup label="country">
        <option>us</option>        
    </optgroup>

</select>

Upvotes: 2

Views: 890

Answers (3)

Abhitalks
Abhitalks

Reputation: 28397

$('select').find('option').not(':first').unwrap();

You have to unwrap the option not the parent of the option!

Your updated fiddle: http://jsfiddle.net/Lq5dN/10/

Upvotes: 3

Greenhorn
Greenhorn

Reputation: 1700

try:

$(function(){
   $('select').find('optgroup option').unwrap()
})

Demo Fiddle

Upvotes: 2

codingrose
codingrose

Reputation: 15699

Try:

$(function(){
   $('optgroup').find('option').unwrap();
});

DEMO here.

Upvotes: 1

Related Questions