javascript - Image hover and replace - jquery -


greetings have been trying hours , having trouble trying figure out. new javascript , jquery. example have 2 images, black , blue image. have 4 black images when hovered individually, changes img src blue, , goes black when hovered out. trying accomplish when click image/link, stay "blue". change "black" once click on other images.

here have far:
html

<a id="catlnk1" href="javascript:void(0);" onclick="loadcat(1)"><img src="/images/blogpage/published.png" onmouseover="this.src='/images/blogpage/published-active.png'" onmouseout="this.src='/images/blogpage/published.png'" alt="published" /> 

script

function loadcat(num)  { if (num == 1){  $("#catlnk" + num).attr("src","/images/blogpage/published-active.png");  } else if (num == 2){  $("#catlnk" + num).attr("src","/images/blogpage/upcoming-active.png");  } else if (num == 3){  $("#catlnk" + num).attr("src","/images/blogpage/opening-active.png");  } else if (num == 4){  $("#catlnk" + num).attr("src","/images/blogpage/job-active.png");  }  } 

sorry if code messy, i'm new this.

it bad idea bind events inline. separate out functionality design , bind events using javascript or jquery.

i using html-5 data-attributes called data-image holds image displayed.

so html this.

html

<a id="catlnk1" href="javascript:void(0);">     <img data-image="published" src="/images/blogpage/published.png" alt="published" /> </a> <a id="catlnk2" href="javascript:void(0);">     <img data-image="upcoming" src="/images/blogpage/upcoming.png" alt="upcoming" /> </a> <a id="catlnk3" href="javascript:void(0);">     <img data-image="opening" src="/images/blogpage/opening.png" alt="opening" /> </a> <a id="catlnk4" href="javascript:void(0);">     <img data-image="job" src="/images/blogpage/job.png" alt="job" /> </a> 

js

var url = "/images/blogpage/"; $('[id^=catlnk]').on({     click: function (e) {         var $currimg = $(this).find('img');         $currimg.attr('src', function (_, src) {             return url + $(this).data('image') + '-active.png';         });          // images          $allimages = $('a > img');         $allimages.not($currimg).each(function () {             $(this).attr('src', function (_, src) {                 return url + $(this).data('image') + '.png';             });         });         $currimg.addclass('clicked');         $allimages.not($currimg).removeclass('clicked');     },     mouseover: function () {         var $img = $(this).find('img');         $img.attr('src', function (_, src) {             return url + $(this).data('image') + '-active.png';         });         $img.addclass('active');     },     mouseleave: function () {         var $img = $(this).find('img');         $img.attr('src', function (_, src) {             return url + $(this).data('image') + '.png';         });         $img.removeclass('active');     }  }) 

check demo


Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -