Each do js problem

I have a bit of code which displays subcomments nicely and a bit of js
to hide them:

<% comment.subcomments.each do |subcomment| %>

<%= subcomment.body %>

<%= subcomment.created_at %><%= subcomment.user_id %>

<% end %>

This is the js

The subcomments on the first comments subcomments hide nicely but the
subcomments button on the second and subsequent comments only bring back
the subcomments on the first comment and has no effect on it’s own
subcomments.

Can anybody see where I am going wrong?

“The subcomments on the first comments subcomments”

“the subcomments on the first comment and has no effect on it’s own
subcomments”

lol :slight_smile:

In jquery there is nice method toggle.

.toggle( showOrHide )
showOrHideA Boolean indicating whether to show or hide the elements.
With no parameters, the .toggle() method simply toggles the visibility
of elements:

$(‘.target’).toggle();

http://api.jquery.com/toggle/

use it, it will simplify you code and maybe you’ll find out solution on
your own.


On May 10, 1:30pm, Neil B. [email protected] wrote:

<div id="commenter" >
 <p><%= subcomment.created_at %><%= subcomment.user_id %></p>
</div>
</div>

<% end %>

Is this repeated for each comment? have multiple things on the page
with the same id is a no-no: ids should be unique

Fred

Eugen C. wrote in post #997765:

.toggle() | jQuery API Documentation

use it, it will simplify you code and maybe you’ll find out solution on
your own.
Never used jquery before, I see it’s simplicity compared to straight js

I now have

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

<%= comment.body %>

<%= comment.story_id %> <%= comment.id %>

Hide
<% comment.subcomments.each do |subcomment| %>

<%= subcomment.body %>

<%= subcomment.created_at %><%= subcomment.user_id %>

<% end %>
<% end %>

with

It still gives the same problem i.e all Hide buttons affect the first
comments subcomments

Frederick C. wrote in post #997768:

Is this repeated for each comment? have multiple things on the page
with the same id is a no-no: ids should be unique

Fred

This is the full code

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

<%= comment.body %>

<%= comment.story_id %> <%= comment.id %>

Hide
<% comment.subcomments.each do |subcomment| %>

<%= subcomment.body %>

<%= subcomment.created_at %><%= subcomment.user_id %>

<% end %>
<% end %>

Yes it is repeated for all comments. Both comments and subcomments have
individual id’s I don’t see you point.

On 10 May 2011, at 17:56, Neil B. [email protected] wrote:

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

<%= subcomment.created_at %><%= subcomment.user_id %>




<% end %>

<% end %>

Yes it is repeated for all comments. Both comments and subcomments have
individual id’s I don’t see you point.

Well for example you have multiple divs with id subcomNormal, and your
onclick does getElementById(‘subcomNormal’), which can’t know that you
want (for example) the third item on the page with that id

Fred

On 10 May 2011 17:56, Neil B. [email protected] wrote:

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

<%= subcomment.created_at %><%= subcomment.user_id %>




<% end %>

<% end %>

Yes it is repeated for all comments. Both comments and subcomments have
individual id’s I don’t see you point.

To check whether your html is valid, view the page in the browser and
view the html using View >> Page Source (or similar). Then copy the
complete text and paste it into the w3c html validator.

Colin

Jeffrey L. Taylor wrote in post #997862:

"> > > HTH, > Jeffrey

Thanks that did the trick but I applied it to

Neil

Quoting Colin L. [email protected]:

<div id="commenter" >
<p><%= subcomment.created_at %><%= subcomment.user_id %></p>
</div>
</div>

<% end %>

<% end %>

Yes it is repeated for all comments. Both comments and subcomments have
individual id’s I don’t see you point.

They don’t have different ids. Every remark has the ID, ‘remark’.
Maybe you
intended something like:

or simply:

">

HTH,
Jeffrey

Neil B. wrote in post #998024:

Jeffrey L. Taylor wrote in post #997862:

">

I rebuilt the app in Rail 3 and the above no longer works. Has anybody
an idea why? Or What can I use instead?

On Aug 2, 12:22pm, Neil B. [email protected] wrote:

Neil B. wrote in post #998024:

Jeffrey L. Taylor wrote in post #997862:

">

I rebuilt the app in Rail 3 and the above no longer works. Has anybody
an idea why? Or What can I use instead?

How has the output changed?

Fred

Frederick C. wrote in post #1014673:

On Aug 2, 12:22pm, Neil B. [email protected] wrote:

Neil B. wrote in post #998024:

Jeffrey L. Taylor wrote in post #997862:

">

I rebuilt the app in Rail 3 and the above no longer works. Has anybody
an idea why? Or What can I use instead?

How has the output changed?

Fred

Simply rejects _#{comment[:id]} No output i.e no toggling atall not
even on the first comment.

I’ve attached _subcomment.html.erb

Neil

On Aug 3, 11:30am, Neil B. [email protected] wrote:

Sorry the attachment is now correct, Id missed out the _#{comment[:id]}

it’s not in a <%= tag anymore

Fred

Sorry still stuck. If you look at the attached _subcomment.html.erb you
will see that I have put the jQuery inside the each loop and it
obviously causes problems because the jQuery is repeated. It’s inside
because I get an error on the comment variable otherwise.

Any ideas

Neil

Sorry the attachment is now correct, Id missed out the _#{comment[:id]}

Neil