2

I do not understand why I have to click on the link twice in JQuery:

$(function(){
    $("#more_reviewers").click(function(){
        $("#reviewer_list").html("<%= escape_javascript(render 'restaurants/restaurant_reviewer_list', :venue => @restaurant, :limit => @restaurant.reviews.count) %>");
    });
});

When I click, the view overrides the limit on the reviwers shown and shows all the reviewers. What I don't get is why I need to click on the same button twice. None of the other answers to similar questions have worked for me because the problem involves cookie reliance, or somehting else which I'm not doing.

I just tested one more thing: when I put an alert showing the event type, it shows nothing the first time (as if the JS script wasn't bound to the link) then the second time it alerts with the right event type ("click").

The link with ID "more_reviewers" is as follows in rails:

<%= link_to "more", "#", :id => "more_reviewers", :remote => true  %>

The link shows the right items, but only after being clicked twice (the first time, nothing appears to change on the page)

Help will be very appreciated.

7
  • Is '#more_reviewers' inside an <a> element ? Commented Mar 21, 2013 at 7:03
  • Yes @Sagar_Jackey. I edited my question with the link information, here it is for you: Commented Mar 21, 2013 at 7:04
  • Remove :remote => true from your link_to. That's only useful if you want to use Rails' built in functionality for Ajax, but you are writing the Javascript bindings yourself, so it's useless and causes this unwanted effect of having to click twice. Commented Mar 21, 2013 at 7:56
  • Hi Mischa thanks for the response. But now the effect without :remote -> true is that a) if I put nothing in the destination part, it reloads the page or b) if I put javascript:void(0) it does nothing. Commented Mar 21, 2013 at 8:00
  • Where is this javascript code exactly? Commented Mar 21, 2013 at 8:04

6 Answers 6

4

Your show.js.erb should only contain:

$("#reviewer_list").html("<%= escape_javascript(render 'restaurants/restaurant_reviewer_list', :venue => @restaurant, :limit => @restaurant.reviews.count) %>");

Right now it you're binding a click event to a link you have already clicked, which means you have to click it again.

Sign up to request clarification or add additional context in comments.

8 Comments

OK Mischa, first of all it worked. Thank you. Second, I'm confused. The Show view contains, among other buttons, the "more_reviewers" link. I thought when I load the show view, it also loads all the scripts in show.js.erb. It made sense that I should have an onclick listener (or equivalent) ready to react when there is a click. Here, it seems to me that by clicking on the link I am calling on all the show.js.erb code. What if I had other code not related to the link, say a listener for a click on a different link on the page?
The reaction is also delayed with this solution. It responds to the click the first time, but the response is slower than after the second click of the previous implementation. Is this because the entire page is being reloaded remotely?
No, it does not load show.js.erb. You should only use .js.erb files to return content after an Ajax call. For normal javascript use app/assets/javascripts/applition.js.
Yes, it's doing an Ajax call, so the response is delayed.
It seems all that code should be in your application.js file not in show.js.erb. Sorry, but I can give a precise answer, because it's not clear what you are trying to do and I can't see your code.
|
1

Just had an issue like this and you should be able to solve it like this (this enables you to do other js stuff in the same file and not in the application.js):

$(function(){
  $("#more_reviewers").bind('ajax:complete', function() {
    $("#reviewer_list").html("<%= escape_javascript(render 'restaurants/restaurant_reviewer_list', :venue => @restaurant, :limit => @restaurant.reviews.count) %>");
  });
});

Comments

0

Remove that '#' in your html code. Replace it with javascript:void(0);

<%= link_to "more", "javascript:void(0);", :id => "more_reviewers", :remote => true  %>

5 Comments

Unfortunately this doesn't work - the link ceases to respond entirely!
then try just removing the '#' like this: <%= link_to "more", "", :id => "more_reviewers", :remote => true %>
Still the same problem. The click is registered but nothing happens the first time; it does register the click in the browser console. The second time, the click gets registered AND I see the proper behavior on the screen.
Try this : my first answer without the :remote tag <%= link_to "more", "javascript:void(0);", :id => "more_reviewers" %>
Hi Sagar just tried that and nothing happens. The link doesn't register a click at all.
0

This also will work.

$("#more_reviewers").click(function(e){
    e.preventDefault();
    #.....
});

3 Comments

Thanks for helping out However, this yields the same behavior as before - I need to click twice!
You want to simply update the review_list on clicking that button? If so, can you try by changing the link_to into button tag.
Still the same problem with a button. The click is registered but nothing happens the first time; it does register the click in the browser console. The second time, the click gets registered AND I see the proper behavior on the screen.
0

try:

<%= link_to "more", "", :id => "more_reviewers", :remote => true  %>

in my case

<%= link_to "more", "#", :id => "more_reviewers", :remote => true  %>

works too.

2 Comments

Hi Shweta, I tried this and obvs it didn't work. Which makes sense becaue the id is a DOM element ID not a param.
Thanks Shweta. I tried both these solutions (based on other postings) and the problem persists: I always havve to click twice.
0

In Your Java Script Try including this:

<%= javascript_include_tag "jquery-1.8.3", "jquery.validate" %>

I think this will solve your problem as I got solutions using this

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.