Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Jquery to append form field's value to form action?

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?

like image 720
John S Avatar asked Sep 20 '25 13:09

John S


1 Answers

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;
    });
});
like image 85
Rory McCrossan Avatar answered Sep 22 '25 06:09

Rory McCrossan