Support

Account

Home Forums Front-end Issues Javascript API: Read radio value

Solving

Javascript API: Read radio value

  • Trying to get the value of an ACF radio field in my JS like so:

    acf.getField('radio_field_name').val();

    However, this returns undefined, although the field is found. Also tried to get the value with getValue() with the same result. What is the current best practice to get a radio field value?

    (I assume the answer has changed since this question was asked.)

  • It seems I’m also having trouble with other methods on the field not working, such as show() and hide().

  • You need to use the field key, the field name will not work.

  • Thanks for the quick reply. I just tried it. Seems like no methods work on the instance found by the acf.getField() function, even when using the field key.

    Notably, the getField method always finds the same field, no matter whether I’m using the name or key, but the methods on it always return undefined, though the prototypes are available.

    Might this be a bug or am I just doing something horribly wrong?

    However, I got it to work by using the acf.getField() method: acf.getFields({name: 'my_field_name'})[0].val() and acf.getFields({key: 'my_field_key'})[0].val().

  • are you on a field where an acf field gorup or form is being shown?

    
    var field = acf.getField('field_XYZ123');
    console.log(field.val());
    

    https://www.advancedcustomfields.com/resources/javascript-api/#acf.field-val

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

The topic ‘Javascript API: Read radio value’ is closed to new replies.