Topic: Toggle elements

Hi everyone,

I have an un-ordered list as such:

<ul id="activity_<%= a.id -%>">
    <li><%= a.title -<li>
    <li><%= link_to_remote 'Learn more', {:controller => 'activities', :action => 'view', :id => a.id}, {} -%></li>
    <li class="detail" style="display: none;"><%= a.some_value -%></li>
    <li class="detail" style="display: none;"><%= a.some_other_value -%></li>
</ul>

I would like to toggle the visibility of the <li>s that are classed 'detail' when the 'Learn more' link is clicked for the corresponding list.

My controller sets up a variable with the ID of the <ul>, which is 'activity_' + params[:id]. When I do

page.select(@activity_id + ' li.detail').all('details') do |element, index|
  page.toggle element.id
end

it doesn't work.

What am I doing wrong?

Thanks,

Brandon Taylor
Sr. Developer

Dell

Re: Toggle elements

See this thread

Re: Toggle elements

Sweet. Now that's working!

Next stupid question:

I would like to change the HTML of the link that toggled the elements when clicked. When I try to assign a variable to the innerHTML of an element by using:

@link_text = page['activity_' + params[:id] + '_learn'].innerHTML

the page doesn't error out, but also won't work.

Should I just pass in the ID of the element I need to manipulate to an external JavaScript function using 'call'? It seems very difficult to get attribute values of DOM elements within RJS and do something conditionally.

Thanks for the help with the array, I would never have gotten that one!

Brandon Taylor
Sr. Developer

Dell