Bablu Ahmed
Bablu Ahmed

Reputation: 5010

Jquery id property value is undefined

How can I access id property value of the selected element using the find method in jquery. I am trying like this

let attrId = $(this).prev().find(".selectpicker");
let  attrId2 = attrId.attr("id");
alert(attrId2);

But getting undefined

Updated:

Html:

<div>
    <select name="APPLIEDSHIP" id="APPLIEDSHIP_ID" class="selectpicker bs-select-hidden" multiple="" data-actions-box="true" data-live-search="true" data-placeholder="No Filter" data-width="170px" data-size="5">...</select>
</div>

<button style="margin-left: 5px; margin-top: 5px;" type="button" class="btn btn-info btn-xs">
    Print Id Of Selectpicker
</button>

Using the above button I want to access select element's id

Can anyone help me?

Thanks

Upvotes: 4

Views: 91

Answers (2)

Farzaneh Talebi
Farzaneh Talebi

Reputation: 915

You can use $(this).prev(".selectpicker") to find previous element of clicked element.

Please see this Example:

https://jsfiddle.net/wjrmzs8b/

Upvotes: 1

Jeto
Jeto

Reputation: 14927

Edit (after your own): your selector should work.

Demo:

$(function () {
  $('.btn-info').on('click', function () {
    let attrId = $(this).prev().find('.selectpicker');
    let attrId2 = attrId.attr("id");
    alert(attrId2);
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
  <select name="APPLIEDSHIP" id="APPLIEDSHIP_ID" class="selectpicker bs-select-hidden"></select>
</div>

<button type="button" class="btn btn-info btn-xs">Print Id Of Selectpicker</button>

Upvotes: 2

Related Questions