4

I have a simple form with a few textboxes on it. I would like to capture the input of the textboxes, modify it slightly and then append it to the form action URL.

  $('.myBox').on('change', function (event) {
        var myVal = $(this).val();
        $('form').attr('action').appendTo("&MyVal="+myVal);
    });

The above code doesn't work because there is no appendTo to the attr value. Is there another way to accomplish this?

1
  • Why do you want/need to do this? What's the actual goal? Commented Oct 17, 2013 at 15:07

3 Answers 3

11

Your syntax isn't quite right as you want to update the value of the action attribute, not append an element. Try this:

$('.myBox').on('change', function (event) {
    var myVal = $(this).val();
    $('form').attr('action', function(i, value) {
        return value + "&MyVal=" + myVal;
    });
});
Sign up to request clarification or add additional context in comments.

8 Comments

Wont the get parameter be added each time the select box has changed, making redundancy in the action url ? (well latest param should override the first ones)
@f00bar it will indeed. I'll leave it to OP to fix if that's unintended behaviour.
And actually for whatever reason the example is not "updating" the form action. Is your example suppose to?
@JohnS what was the typo?
The ")" after the return value
|
2

You can try this one:

$("form").attr('action',$("form").attr('action')+ "&MyVal=test");

Comments

0

I actually came up with something similar.

            $('.Mybox').on('change', function (event) {
            var action = $('form').attr('action');
            $('form').attr('action',action+"&test");
        });

But I think I like yours better. Thanks

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.