All of the helpers described here are available in templates, provided that you declare the use of the
<script> tag. Listing 11-1 demonstrates using this helper.
link_to_function() helper does exactly that, as shown in Listing 11-2.
echo link_to_function('Click me!', "alert('foobar')") => <a href="#" onClick="alert('foobar'); return none;">Click me!</a>
As with the
link_to() helper, you can add options to the
<a> tag in the third argument.
Note Just as the
link_to() helper has a
<input type="button">) by calling the
button_to_function() helper. And if you prefer a clickable image, just call
11.1.2. Updating a DOM Element
One common task in dynamic interfaces is the update of an element in the page. This is something that you usually write as shown in Listing 11-3.
update_element_function(). Listing 11-4 shows its use.
update_element_function() keeps the template very readable, as you can see in Listing 11-5.
Listing 11-5 - Options of the
// Insert content just after the 'indicator' element update_element_function('indicator', array( 'position' => 'after', 'content' => "<strong>Data processing complete</strong>", )); // Remove the element before the 'indicator', and only if $condition is true update_element_function('indicator', array( 'action' => $condition ? 'remove' : 'empty', 'position' => 'before', ))
11.1.3. Graceful Degradation
Listing 11-6 - Using the
Note You don't need to include
echo when calling the