Home › Forums › ACF PRO › Programatically control Select2 fields › Reply To: Programatically control Select2 fields
Thanks for the troubleshooting tips Jonathan. I made some tweaks to my selectors and with the following code was able to give the Select2 field focus when a new row is added.
acf.add_action('append', function( $el ){
var $wood = $el.find( '[data-name="bulk_wood"] select');
$wood.focus();
$wood.select2('open');
});
I’m still unable to set the value of the Select2 field though. This next snippets work as expected setting the value of text inputs using data from the previous row. Is it possible to set the value of a Taxonomy field type with the Select2 appearance? I think the AJAX parts are complicating this task.
acf.add_action('append', function( $el ){
var $prev_row = $el.prev();
var $wood = $prev_row.find( '[data-name="bulk_wood"] select' ).val();
var $thickness = $prev_row.find( '[data-name="bulk_thickness"] input' ).val();
$el.find( '[data-name="bulk_thickness"] select' ).append(new Option('text', 'value')); // Not working
$el.find( '[data-name="bulk_thickness"] input' ).val( $thickness ); // Works great
});
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.