Support

Account

Home Forums Backend Issues (wp-admin) Updating field values using the Javascript API

Solving

Updating field values using the Javascript API

  • Attempting to update some field values after taxonomy and post object selections have been made, but the simplest test of:
    acf.getField('custom_price').val(100);
    simply doesn’t work.
    I’ve also tried:
    acf.set('custom_price', 100);
    I’m unable to update any field values using the Javascript API.
    Any ideas?

  • So apparently we can’t use getField with the field name, so instead I try:

    var f = acf.findFields({name:'custom_price'});
    f.val('some val'); // doesn't work
    f.each(function(i,a) {
        a.val('some val'); // doesn't work, a is not a jquery object
    });
    f[0].val('some val'); // doesn't work, f[0] is not a jquery object

    So before I post some of the more complex code I’m using, why does this simple update of a field value not work with the ACF Javascript API?

  • The problem is with acf.findFields({name:name}) or acf.getFields({name:name})

    They are NOT returning jquery objects, whereas getField(key) IS.

    I’ll report as a bug, but in the meantime I’m going to use the keys, but this is annoying as several different custom post types and forms need to use the same field names (but obviously have different keys).

  • This is correct. These function do not return the ACF field objects, only the DOM elements.

    It uses

    
    $fields = $(selector);
    

    You can test this doing something similar without using the ACF API.

    I believe that the only way that you can use acf function on a field is to use the `

    
    acf.getField(FIELD_KEY)
    

    and that if you use the other functions then you need to get the field key from the returned dom elements and use that to get the actual field to use ACF functions to manipulate it.

    Although I am not 100% sure on this, I’m simply going by what it is that ACF gets and returns.

  • I don’t understand why getFields (or findFields) would return an array of different objects to the singular getField object.

    Seems like a mistake/oversight to be honest.

  • I don’t know if it is an oversight or not, you’d have to contact the devs.
    All I can do is look at the code and sometimes figure out what is actually happening. I can’t event say what the difference is in getField() because I had some trouble following it.

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.