It's not a secret anymore!*
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
JavaRanch » Java Forums » Java » JSF
Bookmark "change image on click" Watch "change image on click" New topic
Author

change image on click

Mohan Mehra
Ranch Hand

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

my code:

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

if(y=='asc.gif')
{
alert("hi");
document.getElementById('example').src='../../../images/desc.gif'
}
if(y=='desc.gif')
{
document.getElementById('example').src='../../../images/asc.gif'
}
}


<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>
</h:commandLink>
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15952
    
  19

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: http://aspose.com/file-tools
 
subject: change image on click
 
Similar Threads
need help in jquery
How to set timer for select the time am to pm on front end in a jsp page like calender for date?
resultset is not working properly
JavaScript Calendar Popup
How to print a image file into the printer