Home>Support>State handler, receiving value of the selected option

State handler, receiving value of the selected option

By inya, 9 years ago. Last reply by Greg Priday, 9 years ago.
Notice: This thread is over two years old; the information may be outdated. Please consider creating a new thread if you require free support. If you have an active SiteOrigin Premium license, you can email our premium support desk at [email protected].

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).

This is our free support forum. Replies can take several days. If you need fast email support, please purchase a SiteOrigin Premium license.

  1. 9 years, 7 months ago Greg Priday
    Hi, I Work Here

    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.

Replies on this thread are closed. Please create a new thread if you have a question, or purchase a SiteOrigin Premium license if you need one-on-one email support.

Get The Most Out of SiteOrigin with SiteOrigin Premium

Find Out More