javascript - JS and JQuery, passing arguments to functions -


so started making webpage change , want animate buttons , on. want write function wich takes object (in case ) , declares functions hover-event. function works totaly fine in (not usefull) version:

    function hoverover() {         $(document).ready(function(){             $("#forma").hover(function(){                 $("#buttona").animate({marginleft:"5px", opacity:"1"}, "fast");             }, function(){                 $("#buttona").animate({marginleft:"0px", opacity:"0.5"}, "fast");             });         }); 

but in order use function on multiple buttons, want write this:

    function hoverover(source) {             $(document).ready(function(){             source.parent().hover(function(){                 source.animate({marginleft:"5px", opacity:"1"}, "fast");             }, function(){                 source.animate({marginleft:"0px", opacity:"0.5"}, "fast");             });         });     }     // how want call function multiple buttons     hoverover($("#buttona")); 

i tought work if pass source-variable through functions this:

    function hoverover(source) {         $(document).ready(function(source){ //  <-- here             source.parent().hover(function(source){ //  <-- here                 source.animate({marginleft:"5px", opacity:"1"}, "fast");             }, function(source){ // <-- , here                 source.animate({marginleft:"0px", opacity:"0.5"}, "fast");             });         });     }     // how want call function multiple buttons     hoverover($("#buttona")); 

so problem? know way of coding js not best way , specialy js, html , css there million ways realy started 4 days ago. ;)

thanks

if use jquery might extend prototype, this:

$.fn.hoverover = function(){     this.each(function(){         var $this = $(this);         $this.parent().hover(function(){              $this.animate({marginleft:"5px", opacity:"1"}, "fast");          }, function(){              $this.animate({marginleft:"0", opacity:"0.5"}, "fast");          });     }); } 

and bind element this;

$("#buttona").hoverover(); 

or this:

$(".buttonhover").hoverover(); 

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 -