Saggex
Saggex

Reputation: 3500

How can I call a function with link_to?

How can I call a function from my javascriptfiles using link_to in my view-files?

My script is the following:

var toLeft=function()
{
var sel = document.getElementById("#right");
var lin = document.getElementById("#toleft");
var len = sel.options.length;
var w_controller = lin.w_controller;
var w_id = lin.w_id;
var w_type = lin.w_type;
var w_command = lin.w_command;
alert('call works');
for (var i = 0; i < len; i++) 
    {
    if(sel.options[i].selected) 
        {   
        $.ajax(
            {
            type: w_type,
            url: w_controller+'/'+w_id+'/'+w_command,
            parameters: {to_find_id: sel.options[i].value},
            success:function()
                {
                //Do something on success      
                alert('done');
                }
            });
        }
    }
}

How can I use one link with << to call toLeft which also has some parameters like w_id and so on?

Upvotes: 0

Views: 47

Answers (2)

Ferrakkem Bhuiyan
Ferrakkem Bhuiyan

Reputation: 2783

1st you  add an html attribute on the link_to:

<% @video.phrases.each do |phrase| %> 
  <div class = "span4" id ="comment" >
    <%= phrase.content %>
  </div>
  <%= link_to (image_tag("yourimage.png", :size=> "32x32" ), html => {:onclick => "$('#youId').html('CONTENTS OF HTML');"} :remote => true %>
<% end %>

Javascript from Ruby:

<% @video.phrases.each do |phrase| %> 
  <div class = "span4" id ="comment" >
    <%= phrase.content %>
  </div>
  <%= link_to (image_tag("yourimage.png", :size=> "32x32" ) :remote => true %>
<% end %>

<script type="text/javascript">
  $('a').click(function(){
    $("#youId").html('CONTENTS OF HTML');
  );
</script>

may be it's help you

Upvotes: 0

Marek Lipka
Marek Lipka

Reputation: 51151

ERB:

<%= link_to 'to left', '#', id: 'to-left' %>

JS:

$(document).ready(function(){
  $('#to-left').click(toLeft);
});

Upvotes: 4

Related Questions