How to add onclick event to a link? I need to create a link, which when clicked should be able to set a session variable.

I have used l() function to create the link and tried this but this is not working.

'name' =&gt; l(t($n['name']),'edit_details',array('attributes' =&gt; array('onclick' =&gt;$_SESSION['id']=$n['id'] ))),
Links do not allow any callback action, you can not set a variable on click event.

Instead you can either make it a custom form, where submit button is themed to look like a link instead of link and on form submission set the value in session.

Or other way would be, if its not secured information, you can pass it as query parameter in URL.