Reputation: 608
Here's a contrived example of my code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1"/>
<title></title>
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon"/>
<link rel="stylesheet" type="text/css" href=".css"/>
</head>
<body>
<div>
<a href="http://www.google.com" class="foo">YahOO</a>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
$("document").ready(function() {
$("#foo").trigger('click');
});
</script>
</body>
</html>
I would like to have the link fired immediately upon page load. The above does not work and is incorrect? How could this be done?
Upvotes: 6
Views: 43529
Reputation: 6046
I tried many options, but I found one option which worked for me :
$(document).ready(function(){
$('#upload-file')[0].click(function(){
});
});
Upvotes: 1
Reputation: 1
You have to wait until controls are loaded on the page
add this to the bottom on the page
<script>
$(document).ready(function(e){
$("#whateverElement").click();
});
</script>
Upvotes: -2
Reputation: 337560
You have the class foo
set on your element, not an id
so you need to use .foo
. Also, as @JusticeErolin pointed out, the document ready handler doesn't need quotes. Try this:
$(document).ready(function() {
$(".foo").trigger('click');
});
For reference, $("#foo")
will look for an element with id="foo"
, of which there should only ever be one in your page as ids should be unique.
$(".foo")
will look for elements with class="foo"
, of which you can have as many as you like.
Upvotes: 17