Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By devoted.designer on
Hi there,
sorry but i have 3 questions:
1- How can i trigger ajax on a specifix choice of radio button in a radios list?
Adding the ajax attribute to the form element triggers the ajax function on every value change!
$form['act_type'] = array(
'#type' => 'radios',
'#title' => t('Type'),
'#options' => drupal_map_assoc(array(
t('Art'),
t('Conference'),
t('Music'),
t('Other'),
)),
'#default_value' => empty($form_state['values']['act_type']) ? '' : $form_state['values']['act_type'],
'#required' => TRUE,
'#ajax' => array(
'wrapper' => 'act-type-other-div',
'callback' => 'show_act_type_other_callback',
'effect' => 'fade',
)
);
I just want ajax to be triggered when the "Other" radio is chosen.
2- The same question concerning checkboxes.
$form['act_type'] = array(
'#type' => 'checkboxes',
'#title' => t('Type'),
'#options' => drupal_map_assoc(array(
t('Art'),
t('Conference'),
t('Music'),
t('Other'),
)),
'#default_value' => empty($form_state['values']['act_type']) ? '' : $form_state['values']['act_type'],
'#required' => TRUE,
'#ajax' => array(
'wrapper' => 'act-type-other-div',
'callback' => 'show_act_type_other_callback',
'effect' => 'fade',
)
);
I just want ajax to be triggered when the "Other" checkbox is chosen, although multiple choices can be chosen here.
3- How to get the value of a selected checkbox if it is in a checboxes list?
Thanks in advance for you time and help.
Eagerly waiting reply.
Comments
Using #ajax['trigger_as'] may
Using #ajax['trigger_as'] may do the trick but I suspect you are trying to show/hide a textfield bases on that selection in which case you might want to use #states instead, see http://api.drupal.org/api/drupal/includes--common.inc/function/drupal_pr..., the example module also includes an example for this
You're right! I want to
You're right! I want to show/hide a textfield when the "Other" radio/checkbox is chosen ...
But since i'm these radios and checkboxes lists are in a fieldset ... "#states" doesn't seem to work!
The textfield stays visible at all time in this fieldset.
I've searched for #ajax['trigger_as'] before posting my questions but found no practical examples ... i didn't know how to implement it or get it to work :(
the original code is as follows:
As you can see "step2" is the field set that prevents #states from working right
I guess i figured out the
I guess i figured out the problem that prevents the textfield from hiding and keeps it visible at all time ... but doesn't have a clue how to fix it :S
This is the code from the examples:
working like a charm ...
but adding a container element (div or fieldset) before the "student_type" messes everything up
do you know how to fix that?
and how to use #ajax['trigger_as']? an example of this would be appreciated :)
I use firebug for this, I
I use firebug for this, I inspect the radios for student_type and see what name is used the use that in the code.
You originally asked about checkboxes, the examples in form_examples_states.inc includes an example for that also.
Finally!!! Got it :)
Hey nevets, thanks for your time .... and your help ... I FINALLY GOT IT
you gave me a push to dig deeper into states ...
To fix the above code just add the fieldset name, in which the elements are contained, before the element name to check, thus the code becomes as follows:
As u can see, just adding the fieldset "step2" name before the name of the element to be checked, in the input selector name, fixes the problem ...
THANK YOU .... and hope this helps u as well
Checking if a specific checkbox in a checkboxes list is checked
Facing another problem now!!!
The previous solution worked perfectly with a radio, radios list, and a single checkbox.
But i need to apply it on a multiple choice checkboxes list!
The problem is that the checkboxes list can have multiple values, so how can i check that a specific chechbox is checked.
I'll try to explain more, see, I have 3 choices here.
I want: Selecting the first 2 choices doesn't trigger anything, but selecting the "Other" choice shows the hidden textfield:
How can I check that the 3rd checkbox (in this case "Other") is selected?
If we use 'value', as in the above example, all the checked values are taken into consideration, but i only want to check if "Other" is checked to show the hidden element.
Thanks in advance ...
Eagerly waiting your reply.
Got it!!! AGAIN!!!
HAHAHAHAHA ....
I got it again ... debugged it using my personal favorite, the awesome addon ,"FireBug"
Simply just add the generated checkbox name of "Other" to the input-selector name, and check the 'checked' attribute instead of the 'value' attribute.
The code becomes as follows:
Hope this also helps ...
Happy Programming :)
update of the radio fieldset
For information now there is change,
here is an update of your code
the "step2" has to be remove to make it work
I guess it should be the same with checkboxe.
Anyway thanks, now I can remove my useless jQuery for this one :D