This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have Dawn & David Griffiths on-line!
See this thread for details.
The moose likes JSF and the fly likes change image on click Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "change image on click" Watch "change image on click" New topic

change image on click

Mohan Mehra
Ranch Hand

Joined: Jul 28, 2011
Posts: 72
how to change image after clicking on it using jsf.

my code:

function changeIt()
var theImg = document.getElementsByTagName('img')[1].src;
var x = theImg.split("/");
var t = x.length-1;
var y = x[t];


<h:commandLink actionListener="#{Machine.sortDataList}">
<f:attribute name="sortField" value="getmCode" />
        <hutputText id="code" value="#{msg.code}" />
<div id="example"><h:graphicImage onclick="changeIt()" url="../../../images/asc.gif"></h:graphicImage></div>
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16901

That's not really "using JSF". It's using JavaScript on a JSF View.

You should be able to simplify the JavaScript code considerably, however. Just pass "this" as a parameter to the changeIt() function. Like so:

That should pass the generated IMG tag element to changeIt, eliminating the need for all those messy DOM searches. And, as a side benefit, passing the image as a parameter means that the JavaScript is more likely to be reusable if you have multiple clickable images on the page.

Also, when using JavaScript to search the DOM by ID, remember that the generated HTML IDs are not the same values as the "id" attributes on JSF tags.

Customer surveys are for companies who didn't pay proper attention to begin with.
I agree. Here's the link:
subject: change image on click
It's not a secret anymore!