0

Using Rails, I am submitting a form that generates a new url each time the form is submitted. How can I catch the url of the new form? I tried something like the following but can't get it to work

$('#addSyn').click(function(ev){
   url = $(this).attr('href');
   $('#expURL').text(url);
}); 

HTML/Rails

<section style="width:45%; right:0; position:absolute;">

<%= form_for @exp, remote: true, :id => 'EditForm' do |f| %>
  <%= f.label :Syn %><br>
  <%= f.text_field :syn %><br>

  <%= f.label :Exp %><br>
  <%= f.text_field :exp %><br>

  <%= f.submit "Add Syn", :id=>"addSyn" %>
<% end %>

<div id="ExpURL">

</div>

</section>
10
  • 2
    Are you sure it's the "href" but not "action"? Commented Aug 22, 2013 at 4:02
  • Does the server do a redirect to send the browser to a new response url each time? If so, that probably can not be handled from a Javascript because that Javascript has been unloaded. Maybe with an Iframe if it doesn't violate same origin policy. Commented Aug 22, 2013 at 4:04
  • @Teddy I tried "action" instead of "href" but still nothing. Commented Aug 22, 2013 at 4:09
  • @Paul - I am using rails and I inserted a statement "remote: true". Without it it does a redirect, with that statement it listens to the jquery click and does not do a redirect. Commented Aug 22, 2013 at 4:11
  • give your html like structure Commented Aug 22, 2013 at 4:16

1 Answer 1

1

You only get #addSyn using $(this) inside $('#addSyn').click. And the case seems wrong(if it's not a typo), it should be #ExpURL.

Try:

$('#EditForm').on('submit', function(){
  url = $(this).attr('action');
  $('#ExpURL').text(url);
});

Also you can use prop instead of attr.

Sign up to request clarification or add additional context in comments.

1 Comment

I tried submit instead of click but when I try that then event random text such as .text('foobar') doesn't display. Ignore the casing. That was a typo

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.