tags:

views:

33

answers:

3

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');})
+1  A: 

Why not the easy way:

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

?

jigfox
thx as well!!!!
+1  A: 
$("[name=form]").attr("action", "ShoppingCart.asp?recal=Y");
SimpleCoder
LOL sometimes you overlook the simply stuff, I need some sleep. thx
+1  A: 

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.

Nick Craver
Hey Nick the reason it didn't work is I put my code in the wrong place so of course it wouldn't work but that code that I have seems a little overkill when I can just use other more simpler suggestions, but I did change the selector to yours