jquery - Javascript function that can be used by many elements and then distinguish which element clicked it -


for example have 12 div tags each represent month. when clicked each 1 display div month , hide other months divs

the 12 month divs have class month in common. sections each contain content month have month_section in common , name of month represent uncommon.

my javascript code far:

$("#january,#february,#march,#april").click(function(e)){   $(".month").removeclass("highlight");   $(".month_sections").removeclass("show");   $(".month_sections").addclass("hide");   if(this == "#january"){(this).addclass("highlight");$(".january").addclass("show");}   else if(this == "#february"){(this).addclass("highlight");$(".february").addclass("show");}   else if(this == "#march"){(this).addclass("highlight");$(".march").addclass("show");}   else if(this == "#april"){(this).addclass("highlight");$(".april").addclass("show");} }); 

the if statement not being correctly detected

in event handler trying compare this object string.

the this in context of jquery callback element clicked. if wish compare clicked have use this.id in context of first month equal january. can compare.

if(this.id == "january") {     $(this).addclass("highlight");     $(".january").addclass("show"); } 

there other approaches can take save on code.

$("#january,#february,#march,#april").click(function(e) {     var monthstr = this.id;      $(".month").removeclass("highlight");     $(".month_sections").removeclass("show");     $(".month_sections").addclass("hide");      $(this).addclass("highlight");     $("." + monthstr.tolowercase()).addclass("show"); }); 

i hope useful.


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -