Menu

Want to work with us? We're on the lookout for digital experts.

We're hiring

Issue javascript click event with hidden fields

Web Bureau


06 January 2012 by Web Bureau

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.

Grow your businessStart a project with us today.

This site uses essential cookies for parts of the site to operate and have already been set. Find out more about how we use cookies and how you may delete them. You may delete cookies, but parts of the site will not work.