pejmanjohn
pejmanjohn

Reputation: 1057

Copy form input field's value before submission

I'm guessing I need JS to do this, but here's the situation.

I have the following input box in my form:

<%= f.input :goal_text  %>

I also have this link further down:

<%= link_to "Sign up with Facebook", "http://local.ngrok.com/auth/facebook?g_text=hardcoded" %>

What I would like to happen is that when the link is clicked the current value in goal_text gets set in the link. So if a user had typed "this is it" into the textbox and then clicks the link, it would redirect to:

"http://local.ngrok.com/auth/facebook?g_text=this is it"

Upvotes: 0

Views: 197

Answers (2)

xdotcommer
xdotcommer

Reputation: 793

With jQuery you could do this pretty easily. Given ids on the input box and the link (model_goal_text - you will need to add one on your link_to <%= link_to "Sign up", "http://local.ngrok...", id: 'signup_link' %>

    $('#model_goal_text').change(function() {
        $('#signup_link').attr("href", 'http://local.ngrok.com/auth/facebook?g_text=' + encodeURIComponent($('#model_goal_text').val());`
    });

Replace the name of your model above (or right click and inspect the element to see what the id of your input box is).

Upvotes: 1

Wilson Silva
Wilson Silva

Reputation: 11395

Here's how you might do it:

$('#goal_text').change(function() {
  var goal_text = $('#goal_text').val();
  $('a').attr("href", "http://local.ngrok.com/auth/facebook?g_text="+goal_text);
});

http://jsfiddle.net/dnzGt/

Upvotes: 2

Related Questions