0
<ul class="top singleSelect" id="g-dropdown" data-type="singleSelect" role="listbox">
    <li id="li-0" data-level-id="10001" class="li-level1" tabindex="0">
        <ul id="ul-0">
           <li tabindex="-1" data-level-id="" id="li-0-0" class="li-level2">

I need to select the attribute data-level-id of inner li

Can someone help me to select the element using jquery?

7
  • $("#li-0-0").attr("data-level-id") Commented Mar 14, 2018 at 17:00
  • or data('level-id') Commented Mar 14, 2018 at 17:00
  • 2
    Have you made any kind of effort to do this yourself? Commented Mar 14, 2018 at 17:01
  • Can't use ID of li as it is creating dynamically multiple li's... ID's will keep on continue like li-0-0 li-0-1 li-02. I want select that attribute of all the li's Commented Mar 14, 2018 at 17:02
  • 1
    You must clarify the question. Then the answers will appear. Commented Mar 14, 2018 at 17:05

1 Answer 1

2

//select all children li that have a data-level-id attribute on them
var elements = $('#g-dropdown').find('li[data-level-id]').filter(function() {
  //exclude elements that have a nested li with a level id
  return $(this).find('li[data-level-id]').length < 1;
});

console.log(elements.get());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="top singleSelect" id="g-dropdown" data-type="singleSelect" role="listbox">
  <li id="li-0" data-level-id="10001" class="li-level1" tabindex="0">
    <ul id="ul-0">
      <li tabindex="-1" data-level-id="" id="li-0-0" class="li-level2">
        Find Me
      </li>
    </ul>
  </li>
</ul>

Sign up to request clarification or add additional context in comments.

12 Comments

Please, for the love of jQuery, make it $('#g-dropdown').find('li[data-level-id]')
You could do that, sure. But the scope of the selector is pretty small so the performance different would be negligible
How can i store the value of this function into a variable ?
Are you wanting the variable to contain the elements, or the value of the data fields?
i want to store the element into variable .. select the element with data field and store into a variable
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.