javascript - Proper way to search for elements in divs with given class? -
<div class="therarepets"> <div class="rarepet"> <span>old</span> <a href="http://examplepetshop.com/abc">buy it.</a> </div> <div class="rarepet"> <span>new</span> <a href="http://examplepetshop.com/def">buy it.</a> </div> </div> <a href="http://examplepetshop.com/vwx">outsidelinkdontwant.</a> <div class="thegoodpets"> <div class="goodpet"> <span>old</span> <a href="http://examplepetshop.com/ghi">buy it.</a> </div> <div class="goodpet"> <span>new</span> <a href="http://examplepetshop.com/jkl">buy it.</a> <a href="http://examplepetshop.com/stu">canhavemorewantedlinks.</a> </div> <div class="goodpet"> <span>new</span> <a href="http://examplepetshop.com/mno">buy it.</a> <a href="http://otherpetshop.com/zzz">outerlinkdontwant.</a> </div> </div> <div class="thebadpets"> <div class="badpet"> <span>new</span> <a href="http://examplepetshop.com/pqr">buy it.</a> </div> </div>
i'd search links in divs "chooseme" classes (i add class via javascript divs i'm interested in), , number, or change color - simple example.
these are:
divs rarepet class, has span content of new
and divs goodpet class, has span content of new
i don't want badpet divs, nor divs without new text span.
i managed selection of divs , adding class (i'm not sure if best way):
$('.rarepet:has(span:contains("new"))').addclass('chooseme'); $('.goodpet:has(span:contains("new"))').addclass('chooseme');
but can't reach links these divs. (in example there should 3 divs chooseme class: <div class="pet chooseme">
.) can links whole document:
var petlinks = document.getelementsbytagname('a'); (var i=0;i<petlinks.length;i++){ var href = petlinks[i].href; if(href.indexof('examplepetshop.com') !=-1){ (petlinks[i].href).length; } }
how run getelementsbytagname('a');
class "chooseme"? next not working:
var petlinks = document.getelementsbytagname('.chooseme > a');
and else i've tried gave errors. like:
var wantedlist = document.getelementsbyclassname('chooseme'); var petlinks = wantedlist.getelementsbytagname('a');
don't make more complicated has be. use jquery add class. keep using jquery select now.
$('.chooseme')
this gives divs chooseme class. chain selectors. jquery works similar css in that. lets either use multiple selectors in same statement, either comma separated selects multiple elements or space separated div a
gives tags in div. in case want use:
$('.chooseme a')
after that, map on , return href each element.
$('.chooseme a').map(function(index, link) {return link.href});
Comments
Post a Comment