Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass JavaScript variable to Flask url_for

I have an endpoint that takes a value in the url and produces some content that will be inserted into a div. I want to build the url with url_for using a JavaScript variable. However, $variable1 is passed as a string, rather than the value of variable1. How can I pass the value of a JavaScript variable to url_for?

function myFunction() {
    var variable1 = "someString"
    $('#demo').load(
        "{{ url_for('addshare2', share = '$variable1') }}"
    );
}
like image 823
Lucas Amos Avatar asked Mar 21 '16 23:03

Lucas Amos


1 Answers

Sometimes I use the following workaround with a temporary placeholder string:

var variable1 = "someString";
$('#demo').load(
    "{{ url_for('addshare2', share='ADDSHARE2') }}".replace("ADDSHARE2", variable1)
);

It doesn't feel quite right and I'm still looking for a better solution. But it does the job.

like image 134
Falko Avatar answered Oct 07 '22 03:10

Falko