Support

Account

Home Forums Front-end Issues Use Javascript API to set Post_Object Value Reply To: Use Javascript API to set Post_Object Value

  • Thanks John,

    I noticed that after I entered into the forums and compared to my code and it was a typo here.

    Widening the look at my code:

        acf.add_action('append', function( $el ){
    
    // The Keys
            layoutKey = 'field_5f498c46f5b9f';       // Layout
            fieldKey = 'field_58dd1b4f4b510';     // Post Object, returns a post id
            
    // Vars
    
            var firstFields = acf.getFields({type: 'post_object', key: fieldKey });
    
            console.log ('Fields Loaded ... New Layout Appended');
    
    // The Count / Index of new entry (Remember the first row is 0)
    
            theCount = firstFields.length;
            newEntry = firstFields.length - 1;
    
    // Do we have an object we can work with?  Set a Status -- Yes this works
    
            firstFields[newEntry].showNotice({
            text: 'This is the field we are trying to set',
            type: 'success',
            dismiss: true
        });
    
    // Retrieve value from Previous Layout 
    
        for (i = 0; i < theCount; i++) {
                // Get the value from the previous record
                if (i == (theCount - 2)) {
    
                    priorValue = firstFields[i].val();    // is the post_id of the selected item
    
                    console.log(priorValue);
                }
    
                if (i == newEntry) {
    
    // Try and set the post id to the field
    
                    firstFields[i].val(priorValue);
                    
    // Trying this ... no change.
                    firstFieldsi].trigger('change');
    
     
    
                }
    
    }