Home > Blockchain >  Jquery to get the tittle='Master pogi'
Jquery to get the tittle='Master pogi'

Time:09-22

How can get the value from the tittle from the multiple select dropdown i just need to get the title and insert it into another textbox separated by comma

<ul class="select2-selection__rendered"><li class="select2-selection__choice" title="MASTER POGI" data-select2-id="15"><span class="select2-selection__choice__remove" role="presentation">×</span>MASTER POGI</li><li class="select2-selection__choice" title="MARTIN MANALOTO" data-select2-id="16"><span class="select2-selection__choice__remove" role="presentation">×</span>MARTIN MANALOTO</li><li class="select2-search select2-search--inline"><input class="select2-search__field" type="search" tabindex="0" autocomplete="off" autocorrect="off" autocapitalize="none" spellcheck="false" role="textbox" aria-autocomplete="list" placeholder="" style="width: 0.75em;"></li></ul>

enter image description here

CodePudding user response:

you can do something like this

const arr = [];
document.querySelectorAll('li[title]').forEach(element => {
    arr.push(element.title);
})

document.querySelector('.select2-search__field').value = arr.join(',')

CodePudding user response:

// Array of titles
let titles = [];

// Collect titles
$(".select2-selection__choice").each((i, element) => {
    titles.push($(element).attr('title'));
});

// Insert it into another textbox separated by comma
$("#anonerTextbox").val(titles.join(', '));

Working example: https://jsfiddle.net/d46bzw8s/1/

CodePudding user response:

    <ul class="select2-selection__rendered" id="selectTitle">
      <li class="select2-selection__choice"  title="MASTER POGI" data-select2-id="15">
        <span class="select2-selection__choice__remove" role="presentation">×</span>MASTER POGI
      </li>
   </ul>

add a unique id to select and then you can either define onclick evnt function or you put it on document ready function.

<script>
  $(document).ready(function() {
    $('#selectTitle .select2-selection__choice').click(function() {
          title =   $(this).attr('title');
          textarea_val = $("#textbox").val()
          if (textarea_val != '') {
             textarea_val  = textarea_val  ',' title ;
          } else {
             textarea_val  = title ;
          }
          $("#textbox").val(title);
      });
  });
</script>
  • Related