javascript - search through node list of elements using regex -
var spans = document.queryselectorall('#divconfirm span'); spans.length = 43.
here spans looks like
spans: [<span id="ctl00_cphbody_lbltitleorder" style="color:#003300;font-family:verdana;font-size:18px;">reservation confirmation</span>, <span id="ctl00_cphbody_lblordernumber" style="color:#003300;font-family:verdana;font-size:20px;">reservation number: 604905</span>, <span id="ctl00_cphbody_lbldate" style="color:#003300;font-family:verdana;font-size:18px;">excursion date: 8/1/2016</span>...
this list of spans contain data , each have id
attribute. regex search id
retrieve corresponding data point.
i e.g. spans.queryselectorall("spans[id*='ordernumber']")[0].
but since elements span
not seem work.
how can search through spans based on regex? considered converting spans node list array google says cannot use regex indexof
, using queryselectorall()
node list , regex *=
seems way go.
so example, how retrieve second item in spans, ordernumber
span element?
well, there no queryselectorall
method in nodelist object. so, can't use queryselectorall
on spans
nodelist.
but use hand-written list filters that:
var filtered = array.prototype.filter.call(spans, function (item) { return /ordernumber/.exec(item.id); });
or in es6:
let filtered = array.prototype.filter.call(spans, (item) => /ordernumber/.exec(item.id));
another way store reference #divconfirm
element, , use queryselectorall
on it:
var filtered = divconfirm.queryselectorall('span[id*="ordernumber"]');
Comments
Post a Comment