My intent here is to select the last element that has both the message
and theirs
classes and a data-msgid
attribute:
var theirLastMessage = document.querySelector('#container .message.theirs[data-msgid]:last-child');
console.log(theirLastMessage);
<div id="container">
<div class="message mine" data-msgid="1"></div>
<div class="message theirs" data-msgid="2"></div>
<div class="message mine" data-msgid="3"></div>
<div class="message theirs" data-msgid="4"></div>
<div class="message mine" data-msgid="5"></div>
</div>
However, querySelector
is returning null, when I want it to be returning the div
with data-msgid=4
in this example. Why doesn't this work and how can I get the element I'm looking for?
I'm not sure if my post title is accurate, I don't have my head wrapped around the problem.