arrays - Javascript to select all <h3>'s, get their length, add class if greater than 30 characters -
i have site created using joomla, , i've created blog layout using masonry. right columns using same width it's masonry 3 columns of width. want create function can grab "h3.g-item-title" , store them array; want able use length property determine length of each h3; want test if length of characters greater 30, if so, add additional class h3's parent div in css can create second class give articles longer titles greater width in masonry.
in theory understand how execute each step, putting them getting lost (new learning javascript). in english, here steps believe need, can please critique thinking if needed , me code out?
- create array variable stores h3's desired class.
- then need test length of each of these h3's , store value each correct title.
- then need test if h3 has greater length 30 characters, , if so, add additional class it, if it's not greater 30, nothing added.
here's markup of each article show div structure , class names:
<div class="g-grid" style="position: absolute; left: 15.008px; top: 15.008px;"> <div class="g-block"> <div class="g-content"> <div class="g-array-item"> <div class="g-array-item-title"> <h3 class="g-item-title"> <a href="/writingcommons-1/index.php?option=com_content&view=article&id=1312:data-visualizations&catid=479:stem-technical-writing&itemid=548">data visualizations</a> </h3> </div> <div class="g-array-item-details"> <span class="g-array-item-date"><i class="fa fa-clock-o"></i>thursday, september 15, 2016</span> <span class="g-array-item-author"><i class="fa fa-user"></i>katherine mcgee</span> <span class="g-array-item-category"> <a href="/writingcommons-1/index.php?option=com_content&view=category&id=452&itemid=101"><i class="fa fa-folder-open"></i>writing commons book</a> <a href="/writingcommons-1/index.php?option=com_content&view=category&id=206&itemid=966"><i class="fa fa-folder-open"></i>genres</a> <a href="/writingcommons-1/index.php?option=com_content&view=category&id=479&itemid=548"><i class="fa fa-folder-open"></i>stem/technical writing</a> </span> <span class="g-array-item-hits"><i class="fa fa-eye"></i>1024</span> </div> </div> </div> </div> </div>
the width applied top div class "g-grid", it's element want add class if h3 within has length greater 30. possible , can walk me through coding this?
thanks :)
do this:
array.prototype.slice.call(document.queryselectorall('h3.g-item-title')) .filter( node => node.textcontent.trim().length > 30) .foreach( node => node.classlist.add("g-grid"));
array.prototype.slice.call
dirty trick turn nodelist
array
. in way, access filter
, foreach
methods let filter out nodes lengthier 30 characters , add class them.
note array.prototype.slice.call
trick won't work on ie. instead have wrap every node in nodelist
in array , filter , modify them:
var mynodelist = document.queryselectorall('li'); var myarrayfromnodelist = []; // empty @ first (var = 0; < mynodelist.length; i++) { myarrayfromnodelist.push(mynodelist[i]); // ahhh, push }
this last bit taken here: https://toddmotto.com/ditch-the-array-foreach-call-nodelist-hack/#problem3-separation-of-concerns
Comments
Post a Comment