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" } }); } };
Comments
Post a Comment