xrcwrn
xrcwrn

Reputation: 5325

get all id of same class in jquery

I am trying to fetch the id of all DOM elements with the same class as seen here https://stackoverflow.com/a/20626819/876739

$('.test').click(function () {
    var ids = $("#infolist li div.no").map(function () {
        return this.id;
    }).get();
    alert(ids);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul id="infolist">
    <li>First
        <div class="No" id="25" style="visibility: hidden"></div>
    </li>
    <li>Second
        <div class="No" id="26" style="visibility: hidden"></div>
    </li>
    <li>Third
        <div class="No" id="27" style="visibility: hidden"></div>
    </li>
    <li>Fourth
        <div class="No" id="28" style="visibility: hidden"></div>
    </li>
</ul>
    <div class="test">test</div>

http://jsfiddle.net/xrcwrn/7nLs635r/1/

On alert it is not showing any value.

Upvotes: 1

Views: 1743

Answers (1)

Milind Anantwar
Milind Anantwar

Reputation: 82231

Jquery class selector uses the new javascript method getElementsByClassName if the browser supports it. This method is case-insensitive on quirks-mode pages, and case-sensitive on non-quirksmode (aka standards-compliant) pages.

div elements have class name No and not no . due to which the selector returns no elements in your case:

$("#infolist li div.No");

Demo

Upvotes: 2

Related Questions