2

I'm setting up a page with javascript that should fire on a click event.

It works most of the time except the first time a page loads.

If I refresh it though, it works and the click events fire properly.

 $(document).ready(function() { 
     $(".element").click(function() { alert("This should work"); }
 });

Anybody have an idea where the issue might be?

mosquito242
  • 353
  • 5
  • 17
  • If you refresh the page, does it for the first time you click on the ".element"? – void Oct 26 '14 at 18:15
  • You mean this: `$(".element").click(function() { alert("This should work"); }).click()`? – dfsq Oct 26 '14 at 18:16
  • @dfsq i mean that when it clicks it doesn't work. you can look at it at wewrite-ph.herokuapp.com. If you click on one of the stories, the javascript doesn't work the first time, but if you refresh, it works fine. – mosquito242 Oct 26 '14 at 18:23
  • @mosquito242 : so i think reloading the page means the page is loading for the first time, what exactly is your issue. Is it that the click doesnt work when you open it in a new tab? – void Oct 26 '14 at 18:24
  • 1
    @mosquito242 wewrite-ph.herokuapp.com is working fine for me, when i clicked on "Product Hunt had a hackathon.... ", it worked fine even for the first time... – void Oct 26 '14 at 18:26
  • so it's probably a typo that a closing bracket is missing at the end of the line `$(".element").click(function() { alert("This should work"); }`...? – benomatis Oct 26 '14 at 18:27
  • @void can you try clicking the "Add a new line" button? That should create a form, but it doesn't render for me unless I refresh. – mosquito242 Oct 26 '14 at 18:28
  • 1
    Its working fine @mosquito242, probably their is some issue at your end, try clearing your browser cache or restart your browser... – void Oct 26 '14 at 18:31
  • this solved my problem: http://stackoverflow.com/questions/17881384/jquery-gets-loaded-only-on-page-refresh-in-rails-4-application – mosquito242 Oct 29 '14 at 19:35

1 Answers1

-2

if .element is class then it should probably be work fine.

But if you are trying to give actual element like <a></a> or <b></b> or <div></div> so :

 $(document).ready(function() { 
     $("div").click(function() { alert("This should work"); }                //Work on click on any div in page ( here is no . before div)
 });