Hi,
while trying to implement state handlers into a new widget I am creating, I gut stuck at the following question: is it possible to change the value of a text field using the text of a selected option of a dropdown-field?
For Example:
I have a select-field which shows all the (custom) posts of a certain type
/* get the custom posts of the type "myposts" */ $my_posts = get_posts(array('post_type'=>'myposts','posts_per_page'=>'-1')); $my_post_options = array(); $my_post_options[0] = esc_html( 'Please select...'); if ( $my_posts) { foreach ( $my_posts as $my_post) { $my_post_options[$my_post->ID] = esc_html( $my_post->post_title); } } /* later: create the select-field */ 'my_custom_post_id' => array( 'type' => 'select', 'label' => __('Please select the post:', 'widget-form-fields-text-domain'), 'state_emitter' => array( 'callback' => 'select', 'args' => array( 'my_custom_post_id' ) ), 'options' => $my_post_options ),
Now I want to change the value of a text-field depending on the selected option:
'any_textfield' => array( 'type' => 'text', 'label' => __('Selected Post to be displayed', 'widget-form-fields-text-domain'), 'default' => '', 'state_handler' => array( 'my_custom_post_id[0]' => array('val', 'input', array('nothing selected yet')), '_else[my_custom_post_id]' => array('val', 'input', array( ??? ) ), ), ),
is there any way to get the text of the selected option? (I tryed several things like e.g. array(“jQuery(this).val()”) which was obviously not the way to go).
Hi Inya
Unfortunately that isn’t the intended usage of state handlers, but we have done similar things in our social icons widget. Taking a look at how this works should help a lot.
It basically comes down to using some custom Javascript.
https://github.com/siteorigin/so-widgets-bundle/tree/develop/widgets/social-media-buttons
You can find the Javascript in the js folder and see how this is all implemented in the main widget php file.