Issue javascript click event with hidden fields

by pmcgann 6. January 2012 19:51

While working on a recent project I had an issue where, when I called a click event through javascript the event would not fire and click the element I wished to click below is the function I had for this:

<script type="text/javascript">

     function select(val) {

       document.getElementById(val).click()

     }
</script>

Because the elements I was working with are hidden on the page the javascript would not fire so I replaced the above with the one below:

<script type="text/javascript">

        function select(val) {

           document.getElementById(val).setAttribute("class", "selected");
           var fireOnThis = document.getElementById(val);
           if(document.createEvent){
           var evObj = document.createEvent('MouseEvents');
           evObj.initEvent('click', true, false);
           fireOnThis.dispatchEvent(evObj);
           } else if(document.createEventObject){
           var evObj = document.createEventObject();
           fireOnThis.fireEvent('onclick',evObj);
           }     
     }
</script>

Hope this helps anyone with a similiar problem.

Tags:

Comments are closed

 

Posts By Date

Log in