Set default value for select html element in Jinja template?

Rohan

I'm using flask/jinja in order to make a simple web application. I have a table of records which is taken from a db table, and is called by a webpage which loads the list of records. On each row there is a dropdown list (done using the select HTML tag) on a column.

I realize the below code doesn't do what its supposed to, currently the last option (fourth) is automatically selected because of the selected tag. I've left it in to try show what I'm trying to implement.

Ideally I'd want it to check the current record's status (rec.status in the code below) and depending on that, select the appropriate item in the dropdown.

HTML:

     <tbody>
            {% for rec in records %}
        <tr>
          <td>{{ rec.task }}</td>
          <td>
            <select>
              <option value ="zero" selected={{rec.status==0}}>Zero</option>
              <option value ="first" selected={{rec.status==1}}>First</option>
              <option value ="second" selected={{rec.status==2}}>Second</option>
              <option value ="third" selected={{rec.status==3}}>Third</option>
            </select>
          </td>
          <td><a href={{ "/update_status/"~rec.id}}>Update</a></td>
      </tr>
      {% endfor %}
      </tbody>

Thanks!

Matt Healy

You're on the right track - but currently, you're printing selected in all the options in your select box. You can try something like this to only print selected on the correct option:

    <select>
      <option value="zero"{% if rec.status==0 %} selected="selected"{% endif %}>Zero</option>
      <option value="first"{% if rec.status==1 %} selected="selected"{% endif %}>First</option>
      <option value="second"{% if rec.status==2 %} selected="selected"{% endif %}>Second</option>
      <option value="third"{% if rec.status==3 %} selected="selected"{% endif %}>Third</option>
    </select>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How can I set the default value for an HTML <select> element?

From Dev

Natively set HTML select element to its default value

From Java

Jinja2 template variable if None Object set a default value

From Dev

How to set default value for HTML select?

From Dev

How to set default value for HTML select?

From Java

how to use ng-option to set default value of select element

From Dev

HTML select element - Custom default value? ( Cross-browser solution )

From Dev

Set default Value select AngularJS

From Dev

Set default value for select in angularjs

From Dev

Select default value of <select> element in Django

From Dev

Dynamically set the VALUE of an text-INPUT element to default by clicking SELECT option

From Dev

Dynamically set the VALUE of an text-INPUT element to default by clicking SELECT option

From Dev

Setting <select> element's default value - alternatives

From Dev

Set HTML select's value

From Dev

Twig template set select control to database value

From Dev

How to set default value for HTML drop-down list not from the option element?

From Dev

Set default value of select to null option

From Java

How to set a default value in react-select

From Dev

Set default value of select2 input

From Dev

Set Default/Null Value with Select TagHelper

From Dev

How can set a default value in select tag?

From Dev

Angularjs how to set the default value for select

From Dev

Unable to set default value on AngularJS select box

From Dev

Set a default value in a select with simple array in AngularJS

From Dev

Set default value of select box in symfony

From Dev

Set default select based on php value

From Dev

I cannot set default value in select list

From Dev

how to set a default value for select option

From Dev

Set a default value in a select dropdown using Angular

Related Related

  1. 1

    How can I set the default value for an HTML <select> element?

  2. 2

    Natively set HTML select element to its default value

  3. 3

    Jinja2 template variable if None Object set a default value

  4. 4

    How to set default value for HTML select?

  5. 5

    How to set default value for HTML select?

  6. 6

    how to use ng-option to set default value of select element

  7. 7

    HTML select element - Custom default value? ( Cross-browser solution )

  8. 8

    Set default Value select AngularJS

  9. 9

    Set default value for select in angularjs

  10. 10

    Select default value of <select> element in Django

  11. 11

    Dynamically set the VALUE of an text-INPUT element to default by clicking SELECT option

  12. 12

    Dynamically set the VALUE of an text-INPUT element to default by clicking SELECT option

  13. 13

    Setting <select> element's default value - alternatives

  14. 14

    Set HTML select's value

  15. 15

    Twig template set select control to database value

  16. 16

    How to set default value for HTML drop-down list not from the option element?

  17. 17

    Set default value of select to null option

  18. 18

    How to set a default value in react-select

  19. 19

    Set default value of select2 input

  20. 20

    Set Default/Null Value with Select TagHelper

  21. 21

    How can set a default value in select tag?

  22. 22

    Angularjs how to set the default value for select

  23. 23

    Unable to set default value on AngularJS select box

  24. 24

    Set a default value in a select with simple array in AngularJS

  25. 25

    Set default value of select box in symfony

  26. 26

    Set default select based on php value

  27. 27

    I cannot set default value in select list

  28. 28

    how to set a default value for select option

  29. 29

    Set a default value in a select dropdown using Angular

HotTag

Archive