user357034
user357034

Reputation: 10981

replace action attribute value in form

I am trying to change/replaced the action attribute in a form but am having a problem with the selector. I want to change the action to ShoppingCart.asp?recal=Y

Here is the markup for the form.

<form onsubmit="if (typeof(Update_Hidden_State_Fields)=='function') Update_Hidden_State_Fields(); if (this.submitted) return false; else {this.submitted=true; return true;}" action="ShoppingCart.asp" name="form" method="POST">

Here is the Jquery I have so far.

$("[name='form']")
.attr('action', function(jj,ww){
     return ww.replace('ShoppingCart.asp','ShoppingCart.asp?recal=Y');})

Upvotes: 1

Views: 4755

Answers (3)

Nick Craver
Nick Craver

Reputation: 630379

What you have works, give it a try here: http://jsfiddle.net/nick_craver/Pgt5D/

I think you're missing is the document.ready wrapper, like this:

$(function() {
  $("[name='form']").attr('action', function(jj,ww){
    return ww.replace('ShoppingCart.asp','ShoppingCart.asp?recal=Y');
  });
});

Without running it on document.ready, unless your script is running after that form element in the page, it just won't find any elements to run .attr() on. By wrapping it in $(function() { }); or $(document).ready(function() { }); you're telling it to wait until the DOM is ready, and the element will be there. Also I suggest changing your selector to $("form[name='form']") (if there are other forms with names) to make it much more efficient.

Upvotes: 1

Chris Laplante
Chris Laplante

Reputation: 29658

$("[name=form]").attr("action", "ShoppingCart.asp?recal=Y");

Upvotes: 3

jigfox
jigfox

Reputation: 18185

Why not the easy way:

$("[name=form]").attr('action','ShoppingCart.asp?recal=Y');

?

Upvotes: 1

Related Questions