onmouseover - How to know the sender of a javascript function? -


in html, have images , javascript function.

<img onmouseover="repl()" class="en" ... /> <img onmouseover="repl()" class="fr" ... /> ... 

when user on image. want change header text according language selected.

i need way have "reference" sender of function in javascript. have no idea because have not used javascript years. please me !

function repl() {      // missing solution      // var lang = sender.attr("class"); <= please correct me      var headertext = "";     if (lang == 'fr') {         headertext = "cliquez sur le drapeau"     } else {         headertext = "click on flag"     } 

the best solution bind event using addeventlistener. give images same class, select them, add events. suggest using data-* attributes store language.

<img class="repl" data-lang="en" ... /> <img class="repl" data-lang="fr" ... /> 

then in javascript:

window.onload = function(){// make sure dom ready     // images     var imgs = document.getelementsbyclassname('repl');      // loop on them     for(var = 0, len = imgs.length; < len; i++){         // add event         imgs[i].addeventlistener('mouseover', function(){             // language. "this" element hovered on             var lang = this.getattribute('data-lang');              var headertext = "";             if (lang == 'fr') {                 headertext = "cliquez sur le drapeau"             } else {                 headertext = "click on flag"             }         });     } }; 

demo: http://jsfiddle.net/ct7tj/1/


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -