aspose file tools
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 Groovy Fundamentals video training course this week in the Groovy 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: 16753

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.
Don't get me started about those stupid light bulbs.
subject: change image on click