javascript - All purpose image swap function -


ok, new javascript please keep in mind when answer. looking function can use enable me click image, click image , swap positions on page. example have 3x4 table 12 separate images, click on 'image1' , click 'image3' , 2 swap. solutions have found deal swapping image specified image, not need. hope makes sense.

any suggestions , solutions welcomed me learn! thank you! also, 100% javascript , not jquery.

html:

<table> <tr>     <th id="pos1"> <img src="pic_01.gif" id="pic1" onclick="imageswap()" alt="" border=       height=100 width=100></img> </th>     <td id="pos2"> <img src="pic_02.gif" id="pic2" alt="" border= height=100 width=100>     </img></td>     <td id="pos3"> <img src="pic_03.gif" alt="" border= height=100 width=100></img></td> </tr> 

attempted js:

imgarray[0] = new image(); imgarray[0].src = 'pic_01.gif';  imgarray[1] = new image(); imgarray[1].src = 'pic_02.gif';  imgarray[2] = new image(); imgarray[2].src = 'pic_03.gif';  etc etc...  var var b function imageswap(){  getelementbyid.src = 

not sure @ swap itself

so bored , felt being nice, here's example of image swapping, though made no attempt. try avoid being distracted cute kittens.

the part of code matters script:

var position = null, target = null;  $("img").click(function(){     if ($(this).attr("class") == "border"){         $(this).toggleclass("border");         target = null;         position = null;         return false;     }     else{         $(this).toggleclass("border");         if (position == null){             position = $(this).attr("src");             $(this).toggleclass("position");         }         else{             target = $(this).attr("src");             $(this).attr("src", position);             $(".position").attr("src", target)                 .toggleclass("position");             $(".border").toggleclass("border");             position = null;             target = null;         }     } }) 

the <html> set of images , css adds margins , border image click. in short, code checks whether image has been selected. if has selection border , click again, deselects image. if image clicked hasn't been selected, checks whether position has been set. if position has been set, swaps target (the image clicked) image clicked. otherwise, sets image clicked position (and next click cause images swap).


Comments

Popular posts from this blog

user interface - How to replace the Python logo in a Tkinter-based Python GUI app? -

objective c - Greedy NSProgressIndicator Allocation -

how to set an OCR language in Google Drive -