gaganHR
gaganHR

Reputation: 337

Click events and MouseOut events not working in chrome

Not able to get into the functions for onclick & onmouseout/over events in Chrome and Firefox. Any reasons for not working in Chrome and FF is there a way around for this. These work fine in IE9 and Opera..

Code in html page is shown below:

     <script language="JavaScript" for="SmartGridCell" event="onclick()">
    sg_CellClick(event.srcElement);
</script>

<script language="JavaScript" for="SmartGridCell" event="onmouseover()">
    sg_MouseOverCell(event.srcElement);
</script>  

more click events...

     <script language="javascript" for="optSet" event="onclick()">
    mc_SelectAnElement(this, document.getElementsByName('optSet'));
</script>

<script language="javascript" for="answerChoice" event="onclick()">
    mc_SelectAnElement(this, document.getElementsByName('answerChoice'));
</script>

This is what I have done so far, but I cannot get the fire the event in Chrome...

 <script language="JavaScript">

  var s1=document.getElementsByName('optSet');

  for (var i=0;i<s1.length;i++)
  {
    s1[i].addEventListener("click",mc_SelectAnElement(this, document.getElementsByName('answerChoice')),false);
  }

</script>

Tried this as well, this snippet gets me to the function but the values selected never persists...

 if (document.addEventListener) 
{
    document.addEventListener("click",function (e){
    var srcElement= e.target;
    var tagName= srcElement.tagName;
    if(tagName="optSet")
    {
        mc_SelectAnElement(srcElement, document.getElementsByName('optSet'));
    }
    //mc_SelectAnElement(this, document.getElementsByName('optSet'));
    },true);
}

Thanks-

Upvotes: 2

Views: 1391

Answers (1)

katspaugh
katspaugh

Reputation: 17899

In modern JavaScript it would be something like:

if (document.addEventListener) {
    document.addEventListener('click', function (e) {
        var el = e.target;
        var id = el.id;

        // the "for" attribute refers to an ID
        if (id == 'SmartGridCell' || id == 'SmartGridHeaderCell') {
            sg_CellClick(el);
        }
    }, true);

    /* repeat the same for "mouseover" and "mouseout" */
}

You can leave the old script too, they won't clash.

Why this works in IE – because they invented their own ugly KnockoutJS 20 years ago. And it's non-standard of course (DOM 2 says Reserved for future use for the for and event attributes).

Upvotes: 3

Related Questions