JSF CommandLink onclick event
Note: this is fixed in JSF 1.2 but for those of us not there yet …
The JSF CommandLink (h:commandLink) tag allows a link to act like an html form submit button. Much to my dismay I discovered that this tag doesn’t support the JavaScript onclick event. This is probably because JSF adds functionality for this event to simulate the form submit.
To solve this problem I wrote a JavaScript function that accepts an html element (CommandLink element) and a function I want to run onclick. After receiving these two arguments it will evaluate IF onclick functionality currently exists. If so, then it will ensure the function parameter passed in will be executed before the JSF provided onclick functionality. If not, then only the passed in function will be executed. Below is the JavaScript:
// random method I want to run onclick
someFunction = function() {
alert('jsf ... yeah?');
}
// jsf commandLink onclick hack
jsfCommandLinkHack = function(link, onClickFunction) {
// check the link exists
if (link) {
// get current (JSF) onclick functionality
var clickFn = link.onclick;
if (clickFn) { // add new functionality
link.onclick = function() {
return (onClickFunction() == false) ? false : clickFn();
}
} else {
link.onclick = function() { return onClickFunction(); }
}
}
}
// handler
// element = form element
jsfCommandLinkHack(element, someFunction);
No comments yet
Leave a reply
RSS - Posts