Support

Account

Home Forums ACF PRO jQuery – get ACF values from dropdown options

Unread

jQuery – get ACF values from dropdown options

  • I have a drop down that is dynamically populated and can be searched by entering text:

    <input type=”text” list=”students” id=”student”>
    <datalist id=”students”>
    <?php
    $args = array(
    ‘post_type’ => ‘students’,
    ‘orderby’=> ‘title’,
    ‘order’ => ‘ASC’
    );
    $students = query_posts( $args );
    foreach($students as $student){
    $student_name = get_the_title($student->ID);
    $student_id = get_field(“student_id”, $student->ID);
    ?>
    <option value=”<?php echo $student_name; ?>” data-id=”<?php echo $student->ID; ?>”><?php echo “(ID#” . $student_id . “)”; ?></option>
    <?php
    }
    ?>
    </datalist>

    Then I’m getting the post ID of the selected option in this snippet:
    jQuery(‘#student’).on(‘input’, function() {
    //student var = whatever is in the search box
    var jsStudent = document.querySelector(‘#student’).value;
    //get the postID for the student
    var jsPostId = jQuery(‘#students option’).filter(function() {
    return this.value == jsStudent;
    }).data(‘id’);

    I need to establish one more variable, which is the ACF field ‘student_id’, for the selected option. How can I dynamically get the ACF value for the selected option via jQuery?

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.