Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

querySelectorAll - How to only get elements within a certain div

Tags:

javascript

svg

I would like to limit this selector, to give me result only from inside a div with id "divId1"

var polys = document.querySelectorAll('polygon,polyline');

How can I do this?

like image 424
user2015338 Avatar asked Aug 23 '13 13:08

user2015338


4 Answers

Try the space operator:

var polys = document.querySelectorAll('#divId1 polygon, #divId1 polyline');

See MDN's page on selectors to learn more.

like image 116
tckmn Avatar answered Nov 10 '22 00:11

tckmn


You can call querySelectorAll on the div element instead of the document.

Reference: https://developer.mozilla.org/en-US/docs/Web/API/Element.querySelectorAll

Sample:

var polys = document.getElementById('divId1').querySelectorAll('polygon,polyline');
like image 20
Tibos Avatar answered Nov 10 '22 00:11

Tibos


Try this:

var el = document.querySelector('#divId1');
var polys = el.querySelectorAll('polygon,polyline');

https://developer.mozilla.org/en-US/docs/Web/API/element.querySelectorAll

like image 41
inhji Avatar answered Nov 10 '22 00:11

inhji


var polys = document.querySelectorAll('#divId1 polygon, #divId1 polyline'); 
like image 45
gdoron is supporting Monica Avatar answered Nov 09 '22 22:11

gdoron is supporting Monica