javascript - querySelector syntax for nested elements - Stack Overflow

I'm trying to get the div element that's nested deep within another div, but can't get t

I'm trying to get the div element that's nested deep within another div, but can't get the CSS Selector string to work.

The div I want does not have a unique identifier, but resides deep within another div that has. I've gotten that top level div, but have no clue how to get the nested one.

DOM:

var obj = document.body.querySelector('.qvobject[data-qlikobjectid="XFvnjF"]');
console.log(obj);

var cont = obj.querySelector('.kpi-value');
console.log(cont);
<div class="qvobject" data-qlikobjectid="XFvnjF">
  <div>
    <div>
      <div class="kpi-value">I WANT THIS</div>
    </div>
  </div>
</div>

I'm trying to get the div element that's nested deep within another div, but can't get the CSS Selector string to work.

The div I want does not have a unique identifier, but resides deep within another div that has. I've gotten that top level div, but have no clue how to get the nested one.

DOM:

var obj = document.body.querySelector('.qvobject[data-qlikobjectid="XFvnjF"]');
console.log(obj);

var cont = obj.querySelector('.kpi-value');
console.log(cont);
<div class="qvobject" data-qlikobjectid="XFvnjF">
  <div>
    <div>
      <div class="kpi-value">I WANT THIS</div>
    </div>
  </div>
</div>

The result is that "obj" is the right object, but "cont" is null, so it can't find it.

Share Improve this question edited Apr 12, 2018 at 8:48 BoltClock 725k165 gold badges1.4k silver badges1.4k bronze badges asked Apr 12, 2018 at 8:43 OverrideOverride 2981 gold badge5 silver badges14 bronze badges 5
  • 2 your code, in snippet working, right? – Durga Commented Apr 12, 2018 at 8:47
  • 3 Yeah even without running the snippet that's now been added I can't see how cont could possibly be null in that situation. – BoltClock Commented Apr 12, 2018 at 8:49
  • 2 Seems like you can't reproduce the issue in your question. – Terry Commented Apr 12, 2018 at 8:52
  • Yes it seems to be working here. I can't replicate with this code, because it is "thinned down". My DOM tree is bigger and with different data. It seems I need to review the structure itself, if this works. – Override Commented Apr 12, 2018 at 8:58
  • Check for nested frames or shadowdom. It should work otherwise . – Nish26 Commented Apr 12, 2018 at 9:31
Add a ment  | 

1 Answer 1

Reset to default 2

You can select it using the descendent binator. Just put a space between the parent selector and the child selector.

This makes the traverser go further to any level in the dom to select the desired element.

var element = document.querySelector('.qvobject[data-qlikobjectid="XFvnjF"] .kpi-value');

console.log(element);
<div class="qvobject" data-qlikobjectid="XFvnjF">
  <div>
    <div>
      <div class="kpi-value">I WANT THIS</div>
    </div>
  </div>
</div>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744356636a4570263.html

相关推荐

  • javascript - querySelector syntax for nested elements - Stack Overflow

    I'm trying to get the div element that's nested deep within another div, but can't get t

    8天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信