Werner
Werner

Reputation: 139

Passing multiple variables to javascript function from onclick link

I want to pass variables to a js function:

<script>

(function(d, s, id) {
    var js, fjs = d.getElementsByTagName(s)[0];
    if (d.getElementById(id)) return;
    js = d.createElement(s); js.id = id;
    js.src = "//connect.facebook.net/de_DE/all.js";
    fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));

// async init once loading is done
window.fbAsyncInit = function() {
    FB.init({appId: 1234567890987, status: false});
};

function gogogo(data) {

    FB.ui({
        method: 'feed',
        link: data.url,
        picture: data.picture,
        name: data.title,
        //caption: data.caption,
        description: data.description
    });
}

Gets in here and displayed all except description:

<%= link_to image_tag('icons/social-fb.png'),'#', :onclick=>"gogogo({ url:'#{request.original_url}',title:'#{content.title}',picture:'#{PICTURE_PATH+content.tg_medias.first.media.url(:big)}', description:'#{content.description}' }) ;return false;" %>

content.description is not working.. Thanks

Upvotes: 0

Views: 780

Answers (2)

Pradeep Agrawal
Pradeep Agrawal

Reputation: 346

I have done this in my many projects. So I will suggest you following approach.

  • Put an id on the link
  • add an script tag in the view

Add following code in a script tag in your view

$(document).ready(function($){
    $("a#id_of_that_link").on('click', function(e){
        gogogo(#{@prepared_data.to_json}, true, #{@anything_else.to_json});
    });
});

Now you will get these params at your function. Let me know If you have any confusion.

Upvotes: 0

nvioli
nvioli

Reputation: 4219

You should pass a javascript object as the argument to the gogogo function. Try replacing

gogogo(data=(url:'#{request.original_url}',url:'#{content.title}'));

with

gogogo({url:'#{request.original_url}',title:'#{content.title}'});

I'm assuming the fact that your argument's two keys were both url was a typo.

Upvotes: 1

Related Questions