sramij
sramij

Reputation: 4925

change form variable at onchange event

I got variable myvar in my wtf-form. I would like to set it's value upon the user selects an option in the dropdown list, w/o the need to add a button/input for the user to confirm his selection. So far, I am not able to have a POST event triggered in views.py whenever the user selects an option.

Moreover, setting form.myvar as I do below doesn't actually do anything, and the alert message won't fire either.

My flask template code:

  <!-- extend base layout -->
{% extends "base.html" %}
<script>
$('#sel_id').change(function() {
    window.alert(5 + 6);
});
</script>
{% block content %}
<div>
<form action="{{ url_for('compile') }}" method="POST">
    <dl>              
        <select id="sel_id">            
            <option value="1">1</option>
            <option value="2">2</option>
        </select>
    </dl>
</form>
</div>
<script type=text/javascript src="{{
  url_for('static', filename='jquery.js') }}"></script>
{% endblock %}

Upvotes: 0

Views: 2400

Answers (1)

Solona Armstrong
Solona Armstrong

Reputation: 535

I would write a piece of JavaScript or jQuery to handle this.

$('#sel_id').change(function() {
    var = 4;
});

Also, you will want to name your variables something other than var, which is keyword. Name it something more descriptive.

Upvotes: 2

Related Questions