I have a select form, to which I add a new option on the fly using jQuery. After a new option is created, I want to select the newly created option.
My code for drawing up HTML for select
<div id="_vendor_select">
    <?php
        // This retrieves the post ID based on a given rule
        // As a result, the post with $default_vendor_id will be selected on default
        $default_vendor_id = fnc_get_default_by_posttype( 'vendor' );
    ?>
    <select id="_vendor" name="_vendor" class="form-control">
        <?php
        $posts = get_posts( array(
            'post_type' => 'vendor',
            'posts_per_page' => -1,
            'order' => 'title',
            'orderby' => 'ASC'
        ));
        if( $posts ) :
            foreach( $posts as $post ) :
                if( $post->ID === $default_vendor_id ) { $selected = 'selected="selected"'; } else { $selected = ''; }
                ?>
                <option value="<?php echo $post->ID; ?>" <?php echo $selected; ?>>
                    <?php echo get_the_title( $post->ID ); ?>
                </option>
            <?php endforeach; ?>
        <?php endif; ?>
        <option disabled>--------------------</option>
        <option value="create_new"><?php _e( 'Add a new vendor', 'book-keeper' ); ?></option>
    </select>-->
</div>
My code for creating a new vendor post (which is called after a new vendor post is created)
// Reload _vendor select div without refreshing the page
// At this point, the 'default vendor post' is pre-selected
jQuery('#_vendor').load(location.href+" #_vendor>*","");
// Remove existing select attribute
jQuery('#_vendor').find('option').each(function () {
    jQuery(this).removeAttr('selected');
});
// Select newly created vendor
// result.post_id is the ID of the newly created vendor post, returned from the AJAX function
jQuery('#_vendor').val(result.post_id).change();
jQuery('#_vendor').find('option[value="'+result.post_id+'"]').attr('selected', 'selected');
Here's the intended flow:
- I draw up HTML for selectto display all posts in 'vendor' custom post type, where one of the posts (which I call 'default vendor post') is pre-selected withselected="selected".
- If an optionwithvalue='create_new'is selected, a bootstrap modal is opened to create a new vendor post using AJAX.
- When the new vendor post is created successfully, reload the selectform so that the newly created vendor post is included in theoption.
- The selected="selected"attribute is removed and the newly createdoptionwill have the attributeselected="selected".
Everything works good except for 4. Against my expectation, what actually happens is:
- When page loads, selectform is drawn with displaying all vendor posts and with 'default vendor post' pre-selected
- A new vendor post is created using modal dialog and AJAX.
- The selected="selected"attribute is removed from all<option>s.
- The newly created vendor post <option>is selected.
- selectform is reloaded without refreshing the page, but with the 'default vendor post' selected.
In my code, I put the form reloading code before doing removal/addition of select attribute, but what really happens is vice-versa.
Why is this so? Getting frustrated...
 
    