Daniel Ezra Moraff
Daniel Ezra Moraff

Reputation: 25

jQuery trigger click not working in IE

The following works in Chrome and HTML--it clicks five separate elements on page load. IE8 doesn't throw any error, but the click event does not happen. Any suggestions would be appreciated.

<script>
    window.onload = function(){
    $(document).ready(function(){ 
    var1=document.getElementById("agencyautoclick")
    $(var1).trigger('click');
    var2=document.getElementById("scaleautoclick")
    $(var2).trigger('click');
    var3=document.getElementById("modeautoclick")
    $(var3).trigger('click');
    var4=document.getElementById("infrastructureautoclick")
    $(var4).trigger('click');
    var5=document.getElementById("topicsautoclick")
    $(var5).trigger('click');
    });
    }
</script>

I originally wasn't using jQuery (just used .click()), but that again did not work in IE8.

Upvotes: 0

Views: 3218

Answers (2)

algorhythm
algorhythm

Reputation: 8728

don't do

var1=document.getElementById("agencyautoclick")
$(var1).trigger('click');

do

var $var1 = $('#agencyautoclick');

// OR

var $var1 = document.getElementById('agencyautoclick');

and don't forget the semicolon ";" end the end of EACH complete command (line).

further

window.onload = function(){
    $(document).ready(function() { /* ... */ });
};

is not good. You have to decide if you want to load the function on window-load or if you want to load it on document-ready.

just write

<script type="text/javascript">
    $(document).ready(function() { /* ... */ });

    // OR

    $(window).load(function() { /* ... */ });
</script>

Upvotes: 2

Sam3k
Sam3k

Reputation: 950

All you have written is the same as:

<script>
$(document).ready(function(){ 
  $('#agencyautoclick').trigger('click');
  $('#scaleautoclick').trigger('click');
  $('#modeautoclick').trigger('click');
  $('#infrastructureautoclick').trigger('click');
  $('#topicsautoclick').trigger('click');
});   
</script>

You are not taking advantage of jQuery =)

Upvotes: 3

Related Questions