0

I have a Ruby-on-Rails web application in which I need to pass a variable in to a javascript function. Easy enough if I just want to do something like this:

<a href="#" onclick="javascript:show_details('section_1')">Show Details</a>

However, what I need to do is something like this:

<% 4.times do |n| %>
    <a href="#" onclick="javascript:show_details('section_" + n + "')">Show Details</a>
<% end %>

Where the parameter being passed to show_details would resolve to 'section_1' then 'section_2' then 'section_3' and finally 'section_4'

I assume this is probably pretty easy but so far I'm kind of stuck.

Any suggestions?

Thanks in advance!

3
  • 2
    Do you have an <%= %> thing in rails. If yes, this will work: <a href="#" onclick="javascript:show_details('section_<%= n %>')">Show Details</a> Commented Feb 9, 2014 at 6:22
  • Thanks - I think that will work. For some reason I'm not seeing an 'accept' icon next to your answer. Do you know if I need to do anything to see the icon? Commented Feb 9, 2014 at 7:46
  • I posted it only as a comment (and not as an answer) since I wasn't sure if my suggestion would work (i haven't worked with rails). Commented Feb 9, 2014 at 8:59

2 Answers 2

1

All you need to do is this:

<% 4.times do |n| %>
  <a href="#" onclick="javascript:show_details('section_<%= n %>')">Show Details</a>
<% end %>
Sign up to request clarification or add additional context in comments.

Comments

0

You have create Javascript variable for that and increment that variable try this code:

<%= javascript_tag do %>
var count = 1;

function sectionCall(){
show_details('section_'+count);
count+=1;
}

<% end %>



<a href="#" onclick="javascript:sectionCall()">Show Details</a>

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.