tags:

views:

19

answers:

1

Background:

I want to render this:

<a href="javascript:myfunction('Somevar')"><img src="some.png" width="20" height="20" border="0" /></a>  

in a cakephp app. Note: this link is not associated with a model at all, it's just part of a form going off-site.

I'm unsure of how to proceed: I've seen this on the cakephp site, but with all that goes on in cake, I'm not sure about the whole javascript calling business. What's the best way to create such a thing?

EDIT: Cake 1.2 is what I am working with.

A: 

It's no problem at all to pass a Javascript function as the second parameter to HtmlHelper::link. This should do the trick:

echo $html->link(
    $html->image('some.png'),
    "javascript:myfunction('Somevar')",
    array('escape'=>false)
);

This invokes HtmlHelper::link, passing an image (generated by HtmlHelper::image) as the HTML around which to wrap the <a />, the Javascript function as the link's action, and informing Cake not to escape the HTML you passed.

Update:
Here's the snippet for Cake 1.2:

echo $html->link(
    $html->image('some.png'),
    "javascript:myfunction('Somevar')",
    null, null, false
);

The final false parameter of HtmlHelper::link instructs Cake not to escape the HTML.

Daniel Wright
I've edited this question. I'm using cake 1.2, which I should have specified.
chiggsy
And I've edited my answer to provide a code snippet for 1.2! HTH
Daniel Wright