1

I created a for using handlebars to get the values ​​from my bank, the search is coming correctly but when trying to select one of the city options it returns me undefined

I believe it is something in this style, but could not apply in my code

Handlebars & Jquery: Dynamically generated ids

    $(document).on("click", "[data-trigger='save']", function (evt) {var commentID = $(this).prev().data("comment");});


 {{#each cidade}}
   <div class="card mt-4">
     <label for="bott"><button class="btn" name="cid" id="cid" data-trigger="save">
       <div class="card-body">
          <small>{{estado}} - {{ativo}} </small>
             <input type="hidden" data-comment ="{{@index}}" name="idcid" id="idcid" value="{{_id}}">
                  </div>
                      </button>
                          </label>
                            </div> 
                                  {{else}}
                                    <h4 value="0">Nenhuma cidade registrada</h4>
                                    {{/each}}

         <input type="text" id="cidade" name="cidade" class="form-control" disabled>

The question is to take the id value and the name of the selected button and put the related name in the input text If you need to add information let me know because I'm new here and I'm using google translator for this conversation thanks in advance

  • https://stackoverflow.com/questions/9454645/does-id-have-to-be-unique-in-the-whole-page Don't repeat ids. Use classes with contextual lookups. – Taplar Oct 15 '19 at 20:52
  • can you give me an example of how not to repeat ids since my list is dynamic – Eloysa Consoni Oct 15 '19 at 20:59
  • 1
    Change `id="whatever"` that you have to `class="whatever"` and you no longer repeat ids. Then use https://api.jquery.com/closest to find the contextual parent of both the elements you are concerned with, then https://api.jquery.com/find the other element you want to change, and change it. – Taplar Oct 15 '19 at 21:02

0 Answers0