javascript - switch multiple images on click -


i have code switch image , forth , works fine. however, wondering if there way rewrite script can use in multiple places. code currently:

    function swaparrows(obj) {         var x=document.images              if (x[0].src.match('images/editloadout.png')) {                 x[0].src="images/editloadoutopen.png";             }              else if (x[0].src.match('editloadoutopen.png')) {                 x[0].src="images/editloadout.png";             }     }  <img src="images/editloadout.png" onclick="swaparrows()" /> 

which works 1 specific instance only. work in multiple places different pictures entirely.

pass 2 images in swap function, , use actual clicked object's src:

function swaparrows(obj, i1, i2) {   var src = obj.getattribute('src');    if (src.match(i1))     obj.setattribute('src', i2);   else     obj.setattribute('src', i1); } 

where html is:

<img src="images/editloadout.png"    onclick="swaparrows(this, 'images/editloadout.png', 'editloadoutopen.png')" /> 

example: http://codepen.io/paulroub/pen/goebf


Comments

Popular posts from this blog

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -

java - JavaFX 2 slider labelFormatter not being used -