Change Link href Based on Viewport (Javascript) -


i'm in special scenario need change href of <a> inside <li>, if <li> has child <ul>, , if viewport sub 480px.

i know little writing javascript, i'm thinking how logic work:

  1. is viewport equal or lesser 480px?
  2. if so, find <li>'s directly inside <nav id="navigation1">'s top-level <ul>.
  3. from #2 returns, find contain child <a>.
  4. from #3 returns, find contain child <ul>.
  5. from #4 returns, change href value of <a> in top <li>'s ''#navigation1``.

here's html:

<nav id="navigation1" class="navigation" style="height:auto; z-index:1; overflow:visible">     <ul class="f-fp f-lp" id="fwnav1">         <li class="fwnavitem"><a href="change-this-link.html"><span class="navigation">about</span></a>             <ul class="sub">                 <li class="fwfirstchild fwnavitem"><a href="#"><span class="navigation">about me</span></a></li>                 <li class="fwnavitem"><a href="#"><span class="navigation">about you</span></a></li>                 <li class="fwlastchild fwnavitem"><a href="#"><span class="navigation">about we</span></a></li>             </ul>         </li>         <li class="fwnavitem"><a href="change-this-link.html"><span class="navigation">services</span></a>             <ul class="sub">                 <li class="fwfirstchild fwnavitem"><a href="#"><span class="navigation">what me do</span></a></li>                 <li class="fwlastchild fwnavitem"><a href="#"><span class="navigation">what do</span></a></li>             </ul>         </li>     </ul> </nav> 

(i know, not prettiest html, it's created wysiwyg , have no control on it. sorry.)

the solution must in vanilla javascript, no jquery please.

thanks!

if (window.innerwidth < 480) { // note 1     var links = document.queryselectorall("#navigation1 > ul > li > a");     var link;     (var = 0; < links.length; i++) {         link = links[i];         if (link.parentelement.getelementsbytagname("ul").length > 0) // note 2             link.href = "#navigation1";     } } 

note 1: window.innerwidth doesn't exist in ie8- (you didn't specify browser requirements). since undefined < 480 false, means code won't run there, fine. may want change <= 480 depending on want; question contradictory in regard.

note 2: not 100% point 4.; rather, checks there descendant <ul>, not child. seems fine html, if it's not, need check (either iterate through direct children of link.parentelement , check <ul>, or check results of getelementsbytagname() 1 parentelement identical link.parentelement. unfortunately, there's no standard dom method filter direct childs of given element property).

you should keep in mind runs once, browser resize (or phone orientation change) may change width not cause links updated. issue if requirement change links e.g. related responsive css (i.e. media queries), because css change according new width.


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -