Home > Software engineering >  How to handle 2 different div clicks and get value from attribute data using jQuery?
How to handle 2 different div clicks and get value from attribute data using jQuery?

Time:10-05

I have an autocomplete feature. Where when I click on one of the lists it will affect the disable/enable conditioning of a dropdown.

Its structure is as below. I have div with id #select-ac which is the main trigger. But I also have a trigger for the autocomplete list .table-autocomplete-value which contains some attribute data. I want when I click table-autocomplete-value, I will get the value of the attribute key. But from the code I made, I don't get the value of the attribute key.

*But I want that element inside #select-ac in jQuery. Can anyone help me?

$(document).ready(function() {
  $('#select-ac').on("click", function (e) {
    $('.table-autocomplete-value').on('click', function () {
          let currentIndex = $(this).data('key');
       console.log('the value is: '   currentIndex)
    })
  });
});
.hidden{
  display:none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="value  company-name-form-container" style="margin-left: -10px;">
  <div id="select-ac"></div>
    <div class="form-group">
      <input class="hidden hidden-singleSelectAutoComplete" type="text" name="account-companyName" value="">
      <input type="text" class="form-control" id="aciaccountcompanyName" placeholder="Nama Perusahaan" name="account-companyNameUserInput" value="" hx-trigger="keyup change delay:200ms" hx-get="/company-name-ac" hx-vars="'account-userInput':jQuery('#aciaccountcompanyName').val(),'account-inputName':'account-companyName' " hx-target="#acaccountcompanyName">
      <div id="acaccountcompanyName" class="">
        <table class="table-autocomplete" id="rac55dab6fdf7f344918966039a24093d68">
          <tbody>
            <tr>
              <td class="table-autocomplete-value" tabindex="22" data-key="22" data-industrysize="" data-industrytypeid="" data-letid="">ABADI JAYA PACKING</td>
            </tr>
            <tr>
              <td class="table-autocomplete-value" tabindex="23" data-key="23" data-industrysize="" data-industrytypeid="" data-letid="">ACER STORE</td>
            </tr>
            <tr>
              <td class="table-autocomplete-value" tabindex="24" data-key="24" data-industrysize="" data-industrytypeid="" data-letid="">ADIJAYA BUANA PERKASA</td>
            </tr>
            <tr>
              <td class="table-autocomplete-value" tabindex="25" data-key="25" data-industrysize="" data-industrytypeid="" data-letid="">ADIJAYA TEKNIK</td>
            </tr>
            <tr>
              <td class="table-autocomplete-value" tabindex="26" data-key="26" data-industrysize="" data-industrytypeid="" data-letid="">ADITYA SRIWIJAYA</td>
            </tr>
              </tbody>
        </table>
      </div>
    </div>
</div>

CodePudding user response:

If you delegate from the container AND your autocomplete does not mess up the HTML, then this works

$(function() {
  $('.company-name-form-container').on("click", '.table-autocomplete-value', function() {
    let currentIndex = $(this).data('key');
    console.log('the value is: '   currentIndex)
  });
});
.hidden {
  display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="value  company-name-form-container" style="margin-left: -10px;">
  <div id="select-ac"></div>
  <div class="form-group">
    <input class="hidden hidden-singleSelectAutoComplete" type="text" name="account-companyName" value="">
    <input type="text" class="form-control" id="aciaccountcompanyName" placeholder="Nama Perusahaan" name="account-companyNameUserInput" value="" hx-trigger="keyup change delay:200ms" hx-get="/company-name-ac" hx-vars="'account-userInput':jQuery('#aciaccountcompanyName').val(),'account-inputName':'account-companyName' "
      hx-target="#acaccountcompanyName">
    <div id="acaccountcompanyName" class="">
      <table class="table-autocomplete" id="rac55dab6fdf7f344918966039a24093d68">
        <tbody>
          <tr>
            <td class="table-autocomplete-value" tabindex="22" data-key="22" data-industrysize="" data-industrytypeid="" data-letid="">ABADI JAYA PACKING</td>
          </tr>
          <tr>
            <td class="table-autocomplete-value" tabindex="23" data-key="23" data-industrysize="" data-industrytypeid="" data-letid="">ACER STORE</td>
          </tr>
          <tr>
            <td class="table-autocomplete-value" tabindex="24" data-key="24" data-industrysize="" data-industrytypeid="" data-letid="">ADIJAYA BUANA PERKASA</td>
          </tr>
          <tr>
            <td class="table-autocomplete-value" tabindex="25" data-key="25" data-industrysize="" data-industrytypeid="" data-letid="">ADIJAYA TEKNIK</td>
          </tr>
          <tr>
            <td class="table-autocomplete-value" tabindex="26" data-key="26" data-industrysize="" data-industrytypeid="" data-letid="">ADITYA SRIWIJAYA</td>
          </tr>
        </tbody>
      </table>
    </div>
  </div>
</div>

CodePudding user response:

.on('click') event listener will be registered for all tags viable with the same class variable.

$(function() {

  //Register your event handler for each item with the same class
  $('.table-autocomplete-value').on("click", function (e) {

    //Get the data from the select item
    let itemKeyVal = $(this).data('key');

    //Return (cancel action) if content is empty
    if(!itemKeyVal)
      return;

    //Only for demo purpose
    $('[response-tag]').text(itemKeyVal);

  });

});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<!-- Only for demo purpose -->
<p style="color:red;" response-tag></p>

<div class="value  company-name-form-container" style="margin-left: -10px;">
  <div id="select-ac"></div>
    <div class="form-group">
      <input class="hidden-singleSelectAutoComplete" type="hidden" name="account-companyName" value="">
      <input type="text" class="form-control" id="aciaccountcompanyName" placeholder="Nama Perusahaan" name="account-companyNameUserInput" value="" hx-trigger="keyup change delay:200ms" hx-get="/company-name-ac" hx-vars="'account-userInput':jQuery('#aciaccountcompanyName').val(),'account-inputName':'account-companyName' " hx-target="#acaccountcompanyName">
      <div id="acaccountcompanyName" class="">
        <table class="table-autocomplete" id="rac55dab6fdf7f344918966039a24093d68">
          <tbody>
            <tr>
              <td class="table-autocomplete-value" tabindex="22" data-key="22" data-industrysize="" data-industrytypeid="" data-letid="">ABADI JAYA PACKING</td>
            </tr>
            <tr>
              <td class="table-autocomplete-value" tabindex="23" data-key="23" data-industrysize="" data-industrytypeid="" data-letid="">ACER STORE</td>
            </tr>
            <tr>
              <td class="table-autocomplete-value" tabindex="24" data-key="24" data-industrysize="" data-industrytypeid="" data-letid="">ADIJAYA BUANA PERKASA</td>
            </tr>
            <tr>
              <td class="table-autocomplete-value" tabindex="25" data-key="25" data-industrysize="" data-industrytypeid="" data-letid="">ADIJAYA TEKNIK</td>
            </tr>
            <tr>
              <td class="table-autocomplete-value" tabindex="26" data-key="26" data-industrysize="" data-industrytypeid="" data-letid="">ADITYA SRIWIJAYA</td>
            </tr>
              </tbody>
        </table>
      </div>
    </div>
</div>

  • Related