Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using jquery to change form action URL

Tags:

jquery

forms

I need to change the form action URL depending on the body class of the page. So far I have

$('document').ready(function () {
 if ($('body').hasClass('hosting')) {
      $('#quoteForm').attr('action', 'hostingURL');
      }
});

and that works perfectly. But I need to expand it to include more body classes. I tried

$('document').ready(function () {
 if ($('body').hasClass('hosting')) {
      $('#quoteForm').attr('action', 'hostingURL');
      }
 elseif ($('body').hasClass('workspace')) {
      $('#quoteForm').attr('action', 'workspaceURL');
      }
 else{}
});

but it isn't working. Any suggestions appreciated.

like image 448
Molly Campbell Avatar asked Nov 30 '12 03:11

Molly Campbell


People also ask

How to change action url in jQuery?

on("click", function(e){ e. preventDefault(); $('#contactsForm'). attr('action', "/test1"). submit(); });

How to change action of form using jQuery?

jQuery is just JavaScript, don't think very differently about it! Like you would do in 'normal' JS, you add an event listener to the buttons and change the action attribute of the form. In jQuery this looks something like: $('#button1').

What is form action in JavaScript?

form action javascript. In an HTML form, the action attribute is used to indicate where the form's data is sent to when it is submitted. The value of this can be set when the form is created, but at times you might want to set it dynamically.


1 Answers

add white space in elseif like else if

like image 63
mattn Avatar answered Oct 18 '22 11:10

mattn