Solved

JavaScript: Change matching form action

Posted on 2013-05-27
1
227 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 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Reactjs with .NET 3 80
Link Anchor Not working 9 51
how can i break an array into a couple 5 26
close pop up windows when logout or main window closes 17 56
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-…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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…

730 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