Link to home
Start Free TrialLog in
Avatar of hankknight
hankknightFlag for Canada

asked on

JavaScript: Change matching form action

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

ASKER CERTIFIED SOLUTION
Avatar of David S.
David S.
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial