0

I was looking for a way to make table row clickable.

I found answer here, but it has one quite bad bug. It only lets you open link once. Even if you're redirected, and then you press "back" button, you can't click again.

I personally would put an onclick event on the tr using jQuery. The tr element would look like this:

<tr data-link="<%= edit_scout_path(scout) %>">
   ...
</tr>

And then the associated JavaScript (placed in a file such as app/assets/javascripts/scouts.js) would be something like this:

$("tr[data-link]").click(function() {
  window.location = this.data("link")
})

This would make all tr elements that have a data-link attribute act as if they were URLs in the most unobtrusive way I can think possible.

Only thing that makes it work again is refreshing site, which is really frustrating.

Do you know how may I fix this issue?

Community
  • 1
  • 1
  • It should work on back button. – epascarello Jan 12 '17 at 00:01
  • I suspect that you're not waiting until a load or pageready event before attaching the click handler to your `tr` elements. – coreyward Jan 12 '17 at 00:51
  • What do you mean? When I am using browsers' "previous site" button, it works as it should, but when I am using self-made button, that only redirects to previous site, it doesn't work. `<%= link_to t(:back), current_user, class: "btn btn-lg btn-primary" %>` –  Jan 12 '17 at 10:46

2 Answers2

0

Disclaimer: I may have no idea what I am talking about.

I made a random quote machine that was giving me the same problem you mentioned. I could only click on the "generate random quote" button once. To click on it again I'd have to reload the page. What I did that fixed it was taking out the .onclick(function() and replaced it with .on("click", function() {

Maybe give that a try

simonC137
  • 1
  • 2
0

Are you using turbolinks? If the answer is yes, try using:

$(document).on 'turbolinks:load'

worked for me...

br3t
  • 1,646
  • 2
  • 20
  • 27