tt0686
tt0686

Reputation: 1849

Conditionally render attribute

In JSF, how can I conditionally render an onclick attribute in Facelets.

<div onclick="$(this).applyFancybox({trigger: !$.browser.msie, target: (event.srcElement || event.target)}); return false;">

How to get something like:

<div #{xBean.method eq null ? 'onclick="$(this).applyFancybox({trigger: !$.browser.msie, target: (event.srcElement || event.target)}); return false;' : ''>

Upvotes: 1

Views: 257

Answers (2)

rdcrng
rdcrng

Reputation: 3443

It is as simple as this:

<div onclick="#{true ? 'console.log(true)' : ''}">Your contents.</div>

will render:

<div onclick="console.log(true)">Your contents.</div>

whereas:

<div onclick="#{false ? 'console.log(true)' : ''}">Your contents.</div>

will render:

<div>Your contents.</div>

Obviously, you'd need to change true|false with an actual condition etc. So your example would become:

<div onclick="#{xBean.method eq null ? '$(this).applyFancybox({trigger: !$.browser.msie, target: (event.srcElement || event.target)}); return false;' : ''}">

Upvotes: 1

James Sutherland
James Sutherland

Reputation: 148

use JQuery

if (condition)
{
    $("#myDiv").click(functionA);
}
else
{
    $("#myDiv").click(functionB);
}

Upvotes: 1

Related Questions