I am just in the very beginning stages of learning module development. I am stuck at something very basic. I know it's simple (I think) but I still cant find an answer. Here is an implementation of the form api:

$form['create_fields']['custom_multitext_field'] = array(
'#type' => 'button',
'#value' => t('Some text'),
);

Now say I want to write a JQuery statement to act on clicking the button. I gather that the syntax is something like this:

$("some selector").onclick(function(){alert('test')});

With some selector being a CSS selector. My question is, what would be the selector in the form implemented above, or how do you include the element. Thanks in advance.

Comments

suthagar’s picture

$('#edit-custom-multitext-field').click(function() {
alert('test');
});

Mostly it will replace the underscore into hyphen and start with edit.

Better use firebug to find the selectors.

Rajan M’s picture

Here is an example..

$form['create_fields']['custom_multitext_field'] = array(
'#type' => 'button',
'#value' => t('Some text'),
'#attributes' => array('onClick' => 'alert("hi");' )
);

same way you can assign classes or id for particular form element
eg:
'#attributes' => array('casss' => 'myclass', 'id' => 'my_form_element' );

Best Regards,
Rajan

da_solver’s picture

Hi,
You can use "#prefix" and "#suffix" (form api) to add html markup to your button.

nikifi’s picture

Thanks for all the help! I got it to work.