Solved

JavaScript: Change matching form action

Posted on 2013-05-27
1
218 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
1 Comment
 
LVL 42

Accepted Solution

by:
David S. earned 500 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
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…

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now