• Resolved freshcreate

    (@freshcreate)


    I have a dropdown that is being populated with titles from a custom post type. Using the solution found on this page: http://etzelstorfer.com/en/create-dynamic-ninja-forms-lists-post-types/

    What I need is to be able to set a certain post ID to be the default value in the dropdown. I’ve tried setting ‘selected’ to the post ID, but no luck there.

    Any advice is appreciated! Full jQuery below:

    function prepopulate_forms($data) {
        if($data['label']=='Location') {  
            $args = array(
                'post_type' => 'locations',
                'orderby' => 'name',
                'order' => 'ASC'
            );
            $query = new WP_Query( $args );
            if ( $query->have_posts() ) {
                $data['list']['options']=array();
                while ( $query->have_posts() ) {
                    $query->the_post();
                    $data['list']['options'][] = array(
                            'label' =>  get_the_title(),
                            'value' =>  get_the_title(),
                            'calc'  =>  null,
                            'selected' => null
                        );
                }
            } 
            wp_reset_postdata();
        }
        return $data;
    }
    add_filter('ninja_forms_field','prepopulate_forms');
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)

The topic ‘Using jQuery to select default value in populated dropdown’ is closed to new replies.