Mootools Image Swap Onclick

    Date: 11/17/07 (Javascript Community)    Keywords: html, java

    I'm trying to do an image swap - you click ANY image in the LI and it replaces the FIRST IMG in that LI.

    I've updated the code a couple of times - currently, the following code SWAPS the image, but won't swap back!

    JS:

    	$$('img').addEvent('click', function() {
    		this.getParent().addClass('selected');
    		var src = $E('li img').getProperty('src');
    		var path = src.substring(src.lastIndexOf(/^.*\//),src.length - 7);
    		var img = src.substring(src.lastIndexOf('/'),src.length - 7);
    		var ext = src.substring(src.lastIndexOf('.'),src.length);
    		var newImg = (path + 'on' + ext);
    		this.getParent().firstChild.setProperty('src', newImg);
    	});
    	$$('a.removeItem').addEvent('click', function() {
    		this.getParent().removeClass('selected');
    		var src = $E('li img').getProperty('src');
    		var path = src.substring(src.lastIndexOf(/^.*\//),src.length - 7);
    		var img = src.substring(src.lastIndexOf('/'),src.length - 7);
    		var ext = src.substring(src.lastIndexOf('.'),src.length);
    		var newImg = (path + 'off' + ext);
    		this.getParent().firstChild.setProperty('src', newImg);
    	});
    

    HTML:
    	ul id="whatever"
                li img src="images/image01_off.jpg"
                	Desc Text Other text
                    img src="images/btn_add.jpg" class="btnAdd"
                    img src="images/btn_added.jpg" class="btnAdded"
                    a href="javascript:;" class="removeItem" Remove Item
                /li
                /li
                li img src="images/image02_off.jpg"
                	Desc Text Other text
                    img src="images/btn_add.jpg" alt="Add to Wishlist" class="btnAdd"
                    img src="images/btn_added.jpg" alt="Add to Wishlist" class="btnAdded"
                    a href="javascript:;" class="removeItem" Remove Item
                /li
            /ul
    


    Any ideas?

    Thanks...

    Source: http://community.livejournal.com/javascript/145445.html

« Snowfall script || Javasript and Forms... »


antivirus | apache | asp | blogging | browser | bugtracking | cms | crm | css | database | ebay | ecommerce | google | hosting | html | java | jsp | linux | microsoft | mysql | offshore | offshoring | oscommerce | php | postgresql | programming | rss | security | seo | shopping | software | spam | spyware | sql | technology | templates | tracker | virus | web | xml | yahoo | home