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') }}"
);
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With