18.10.2016 Views

Drupal 7 Module Development

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

JavaScript in <strong>Drupal</strong><br />

ajax_command_before<br />

To add content before an element use ajax_command_before(). It utilizes<br />

jQuery.before() to add content before a selector. Again the $selector, $content,<br />

and $settings arguments are used.<br />

ajax_command_changed<br />

To note that something within a page has changed ajax_command_<br />

changed($selector, $asterisk) can be used. Elements found on the page with the<br />

given jQuery selector will have the ajax-changed class applied to them. $asterisk<br />

is an optional CSS selector, that resides inside $selector. This is used to optionally<br />

append an asterisk to that element.<br />

ajax_command_css<br />

ajax_command_css() uses the jQuery.css() command to update the CSS within<br />

the page. This command takes in $selector and $argument arguments. For<br />

example, changing a page's background color would look as follows:<br />

$commands[] = ajax_command_css('body', array('background-color' =><br />

'#FFFFFF'));<br />

ajax_command_data<br />

jQuery provides a 'data' command to store data within a page outside of element<br />

attributes. The ajax_command_data() function enables <strong>Drupal</strong> AJAX to add and<br />

update data inside jQuerys data cache. The three arguments are:<br />

• $selector, the jQuery element selector<br />

• $name, the name of the data item being accessed<br />

• $value, the value for the item<br />

ajax_command_html<br />

ajax_command_html() utilizes jQuery.html() to update the html for a given selector.<br />

The arguments are $selector (the jQuery selector), $html (the HTML to update the<br />

selector to use), and $settings (optional settings for this command to use).<br />

[ 310 ]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!