user366006
user366006

Reputation:

Convert current page class to select menu 'selected'

I've currently got a jQuery function that converts wp_list_pages to a select menu which navigates to the selected page when you select the page. What I am wondering is can I use the 'current_page_item' class & convert this to set the current page as option 'selected'? My current code is below:

$('li.current_page_item').attr('selected', 'selected');
alert($('ul.selectdropdown')[0].selectedIndex );
$(function() {
    $('ul.selectdropdown').each(function() {
        var $select = $('<select />');
        $(this).find('a').each(function() {
            var $option = $('<option />');
            $option.attr('value', $(this).attr('href')).html($(this).html());
            $select.append($option); 
            $select.change(function() { window.open($select.find(':selected').val(), '_top'); });
        });
        $(this).replaceWith($select);
    });
});

Upvotes: 0

Views: 467

Answers (1)

SeanCannon
SeanCannon

Reputation: 78006

Try this:

$('li.current_page_item').attr('selected', 'selected');
alert($('ul.selectdropdown')[0].selectedIndex );
$(function() {
    $('ul.selectdropdown').each(function() {
        var $select = $('<select />');
        $(this).find('a').each(function() {
            var $option = $('<option />');
            $option.attr('value', $(this).attr('href')).html($(this).html());

            // Check for current page
            if(window.location.href == $(this).attr('href'))
            {
                $option.prop('selected',true).addClass('current_page_item');
            }

            $select.append($option); 
            $select.change(function() { window.open($select.find(':selected').val(), '_top'); });
        });
        $(this).replaceWith($select);
    });
});

Upvotes: 0

Related Questions