?
Solved

JavaScript: Change matching form action

Posted on 2013-05-27
1
Medium Priority
?
235 Views
Last Modified: 2013-05-30
Using only pure JavaScript (no jQuery or other libraries), how can I change the action of all matching forms?

If the action of a form is:
https://example.com/cart3.html

It should be changed to:
http://www.sitemaps.org/

However if the action is anything else it should remain unchanged.

I do NOT want to use jQuery or any other library.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Example</title>
</head>
<body>

<form method="get" action="https://example.com/cart1.html">
<input type="hidden" name="Product_Code1" value="9780231148341">
<input type="hidden" name="Quantity1" value="1">
<input type="submit" value="Submit" />
</form>

<form method="get" action="https://example.com/cart2.html">
<input type="hidden" name="Product_Code1" value="9780231148342">
<input type="hidden" name="Quantity1" value="1">
<input type="submit" value="Submit" />
</form>

<form method="get" action="https://example.com/cart3.html">
<input type="hidden" name="Product_Code1" value="9780231148343">
<input type="hidden" name="Quantity1" value="1">
<input type="submit" value="Submit" />
</form>

<form method="get" action="https://example.com/cart4.html">
<input type="hidden" name="Product_Code1" value="9780231148344">
<input type="hidden" name="Quantity1" value="1">
<input type="submit" value="Submit" />
</form>

<form method="get" action="https://example.com/cart2.html">
<input type="hidden" name="Product_Code1" value="9780231148342">
<input type="hidden" name="Quantity1" value="1">
<input type="submit" value="Submit" />
</form>

<form method="get" action="https://example.com/cart3.html">
<input type="hidden" name="Product_Code1" value="9780231148343">
<input type="hidden" name="Quantity1" value="1">
<input type="submit" value="Submit" />
</form>

<script type="text/javascript">

document.getElementByFormAction('https://example.com/cart3.html').action='http://www.sitemaps.org/';

</script>

</body>
</html>

Open in new window

0
Comment
Question by:hankknight
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 42

Accepted Solution

by:
David S. earned 2000 total points
ID: 39200016
This should do it:
(function(){
  var f=document.getElementsByTagName('form'),i=0,n=f.length,r=/^https:\/\/example.com\/cart3.html/i;
  for(;i<n;i++){
    if(re.test(f[i].action)) {
      f[i].action='http://www.sitemaps.org/';
    }
  }
})();

Open in new window

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question