0

I'm displaying a list of checkboxes using this code :

{% for keyword in keywords %}
    <div class="col m4 s12">
        <p class="range-field">
            <input type="checkbox" id="{{loop.index}}" name="{{keyword.title}}" class="filled-in"/>
            <label for="{{loop.index}}">{{keyword.title}}</label>
            <input type="range" min="0" max="100"/>
        </p>
   </div>
{% endfor %}

After sumbitting the form the server gets the list of selected checkboxes and sends them back so that I can check them back

{% for keyword in selected_keywords %}
    <script>
        $("label:contains('{{keyword.keyword}}')").prev().attr('checked',true);
    </script
{% endfor %}

I'm using contains as selector but it doesn't work for innerHTML. For example if I have java and javascript and i only check java after submitting javascript is also checked. I searched for other related problems and most of them seem to use contains which does not work for specific innerHTML.

Amine Messaoudi
  • 2,141
  • 2
  • 20
  • 37
  • You are using something else ? What framework makes echo for {{ }} ? Don't forget innerHTML is literal or simple string type not object ! – Nikola Lukic Mar 25 '19 at 13:16
  • I'm using Flask. the data is a json array each element is a dict that has 2 keys : keyword and weight. The problem I'm having is in keyword because contains checks both of java and javascript when i only check java – Amine Messaoudi Mar 25 '19 at 13:18
  • You're using `:contains` which does essentially `text().indexOf(keyword) >= 0` - so "java" will be found in "javascript". You need to use something that generates `text() == keyword` - there's duplicates in SO for this. – freedomn-m Mar 25 '19 at 13:22
  • 1
    Possible duplicate of [jQuery :contains(), but to match an exact string](https://stackoverflow.com/questions/7571117/jquery-contains-but-to-match-an-exact-string) – freedomn-m Mar 25 '19 at 13:23
  • https://stackoverflow.com/search?q=%3Acontains+match – freedomn-m Mar 25 '19 at 13:23
  • 1
    This is probably a better solution though - as it creates a new pseudo: https://stackoverflow.com/questions/15364298/select-element-by-exact-match-of-its-content/18462522#18462522 – freedomn-m Mar 25 '19 at 13:25
  • @freedomn-m it works now thanks – Amine Messaoudi Mar 25 '19 at 13:30

0 Answers0