File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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 REST with Spring (video course) this week in the Spring 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: 73
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: 17276

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.

An IDE is no substitute for an Intelligent Developer.
I agree. Here's the link:
subject: change image on click
It's not a secret anymore!