I am trying to get the clicked element's parent's attributes and the elements on which click event should fired are being added dynamically via jQuery. This is my code for click event which is not working at all
$("body").on('click', ".colors_storage input", function (event) {
console.log($(event.target).parents().find('div.colors_storage_outer > select').attr('data-id'))
/*console.log(parent.attr('data-id'))
console.log(parent.attr('name'))*/
})
However, this code works but gives only one id 855 no matter which input elem is clicked.
$("body").on('click', $(".colors_storage input"), function (event) {
console.log($(event.target).parents().find('div.colors_storage_outer > select').attr('data-id'))
/*console.log(parent.attr('data-id'))
console.log(parent.attr('name'))*/
})
and this is my html structure.
<div class="colors_storage_outer">
<select data-id="855" name="colors[]" multiple="" class="form-control colors_storage bulk-colors select2-hidden-accessible" data-placeholder="Colors" tabindex="-1" aria-hidden="true"></select>
<span class="select2 select2-container select2-container--default select2-container--above select2-container--focus" dir="ltr" style="width: 121.25px;">
<span class="selection">
<span class="select2-selection select2-selection--multiple" role="combobox" aria-haspopup="true" aria-expanded="false" tabindex="-1">
<ul class="select2-selection__rendered">
<li class="select2-search select2-search--inline">
<input class="select2-search__field" type="search" tabindex="0" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" role="textbox" aria-autocomplete="list" placeholder="Colors" style="width: 119.917px;">
</li>
</ul>
</span>
</span>
<span class="dropdown-wrapper" aria-hidden="true"></span>
</span>
</div>
<div class="colors_storage_outer">
<select data-id="853" name="colors[]" multiple="" class="form-control colors_storage bulk-colors select2-hidden-accessible" data-placeholder="Colors" tabindex="-1" aria-hidden="true"></select>
<span class="select2 select2-container select2-container--default select2-container--above select2-container--focus" dir="ltr" style="width: 121.25px;">
<span class="selection">
<span class="select2-selection select2-selection--multiple" role="combobox" aria-haspopup="true" aria-expanded="false" tabindex="-1">
<ul class="select2-selection__rendered">
<li class="select2-search select2-search--inline">
<input class="select2-search__field" type="search" tabindex="0" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" role="textbox" aria-autocomplete="list" placeholder="Colors" style="width: 119.917px;">
</li>
</ul>
</span>
</span>
<span class="dropdown-wrapper" aria-hidden="true"></span>
</span>
</div>
.colors_storage inputanywhere in this code..colors_storage input. Mind the space between the words. it is looking for theinputelement inside the element with `..colors_storage' class.colors_storageelement on the page is aselectand aninputcan't be a child of aselect.colors_storageand there is noinputin theselects, so.colors_storage inputdoesn't exist on the page.