Change action (url_for) in javascript using variable

Trying to change action on clicking certain button.

On clicking certain button, I changed Submit button text (submit -> update).

Current action is like

<form method ='POST' action=''>
 // do something
</form>

On changing to update text, I want change this action to url_for using javascript instead ''.

And inside url_for I need to insert variable.

Tried code 1.

  • Give url string to action.
<script>
 var sel = 'space';
 var new_url = "{{ url_for('abc', selected='" + sel + "') }}"
</script>

Using console.log(new_url);
I get /abc/%20%29sel%29%20

What I need was /abc/space

The main problem is that jinja does not allow variables in script section. Looked up stack to solve this problem. But cant find a way to solve this problem..

Tried code 2.

  • Using $.post()
    This executed on everything. ex) onload window, on click submit button etc.

Could anyone help me with changing action in js using variables?