Accessing a variable from an each loop

Hi

If you look at the attached file you will see the jQuery is inside the
each loop, result, it toggles all comments when any hide is clicked.

I want to move the jQuery outside the loop but if I do it throws an
error saying ‘undefined local variable or method `comment’ ’

Is there any way I can pass the comment variable from the loop to the
jQuery.

If I’m going about this the wrong way please let me know.

Neil

PS

This is an extension to this thread

http://www.ruby-forum.com/topic/1680180#new

but I am reposting because the title no longer bears any relation to the
problem I have now.

Neil B. wrote in post #1015384:

Hi

If you look at the attached file you will see the jQuery is inside the
each loop, result, it toggles all comments when any hide is clicked.

I want to move the jQuery outside the loop but if I do it throws an
error saying ‘undefined local variable or method `comment’ ’

Is there any way I can pass the comment variable from the loop to the
jQuery.

No. comment is a local variable and ceases to exist after the loop
finishes. It doesn’t matter anyway, outside the loop what would the
comment variable contain? One comment? How would that help you?

If I’m going about this the wrong way please let me know.

Reverse engineering from your results, this line:

$(“button”).click(function () {
$("<%= “#subcomNormal_#{comment[:id]}” %>").toggle();});

retrieves all the buttons currently on the page, and adds the current
version of the click-function to all the buttons. Therefore, when the
loop ends all the buttons toggle all the comments.

What you want to do
is add the current version of the click-function to only one button.
How do
you identify that button? How about adding the comment id to the
tag:

<button id=<%= comment.id %> >

and then saying:

$(button#<%= comment.id %>).click(…)

Because that selector focuses on a single button, which changes every
time through the loop, the current click-function will only get added to
one button.

You also have to realize that javascript executes in a browser, and
ruby/rails executes on the server. That means that when the following
appears in a .html.erb file:

$(“button”).click(function () {
$("<%= “#subcomNormal_#{comment[:id]}” %>").toggle();});

rails will do the string interpolations #{…} and the <%= %>
substitutions to give you something like:

$(“button”).click(function () {
$("#subcomNormal_3").toggle();});

But that code executes in the browser. And when that js code executes
in the browser, $(“button”) finds ALL the buttons currently on the page.
Writing this in your .html.erb file:

<% @user.comments.each do |comment| %>

Hide

$(“button”).click(function () {…

<% end %>

…does not limit the ‘button’ selector to that tag.

On Aug 8, 12:35am, 7stud – [email protected] wrote:

Neil B. wrote in post #1015384:

If I’m going about this the wrong way please let me know.

Reverse engineering from your results, this line:

$(“button”).click(function () {
$(“<%= “#subcomNormal_#{comment[:id]}” %>”).toggle();});

retrieves all the buttons currently on the page, and adds the current
version of the click-function to all the buttons. What you want to do
is add the current version of the click-function to only one button.

A more jqueryish way of doing things would be to attach the same click
handler to
all buttons, but have that handler find the correct element to hide,
for example if those elements all had class subcomment_container you
might write something along the lines of

$(‘button’).click(function(){$
(this).siblings(‘.subcomment_container’).toggle()})

Fred