Solved

JavaScript: Change matching form action

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…

728 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