Javascript/Jquery get child element, substract string and substitute -


i looked lot, couldn't find solution problem :c i'm not js expert, might easy 1 programmers.

i have list, nav list:

<ul id="menu-navbar">    <li id="menu-item-270-en" class="lang-item">      <a href="site.com/en">        english      </a>    </li>    <li id="menu-item-270-pt" class="lang-item">      <a href="site.com/">        português      </a>    </li> </ul> 

two things: need text (english) <a> inside <li>, subtract , return first 2 letters. want site show en. português text, want show pt instead. replace(); function should do, shouldn't it?

can't insert neither id nor class <a>, because it's generated plugin in wordpress.

for full code, site http://yogmel.com , list on upper right, on navigation bar.

note: english site not functional yet.

thank much!

for requirement, better maintain mapping object instead.

jquery

var langlistmap = [{    text: "english",    shortname: "en"  }, {    text: "português",    shortname: "pt"  }]    $(document).ready(function(){    $.each($("a"), function(i,a){      var sname = getobject($(a).text());      if(sname){        $(a).text(sname.shortname);      }    });  })    function getobject(text){    console.log(text)    return langlistmap.filter(function(a){      return a.text === text.trim();    })[0]  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.1/jquery.min.js"></script>  <ul id="menu-navbar">    <li id="menu-item-270-en" class="lang-item">      <a href="site.com/en">         english       </a>    </li>    <li id="menu-item-270-pt" class="lang-item">      <a href="site.com/">         português       </a>    </li>    <li id="menu-item-270-pt" class="lang-item">      <a href="site.com/">         test       </a>    </li>  </ul>

pure js

var langlistmap = [{    text: "english",    shortname: "en"  }, {    text: "português",    shortname: "pt"  }]    function getobject(text) {    return langlistmap.filter(function(a) {      return a.text === text.trim();    })[0]  }    (function() {    var = document.getelementsbytagname("a");    (var = 0; < a.length; i++) {      var sname = getobject(a[i].innerhtml);      if (sname) {        a[i].innerhtml = sname.shortname;      }    }  })();
<ul id="menu-navbar">    <li id="menu-item-270-en" class="lang-item">      <a href="site.com/en">         english       </a>    </li>    <li id="menu-item-270-pt" class="lang-item">      <a href="site.com/">         português       </a>    </li>    <li id="menu-item-270-pt" class="lang-item">      <a href="site.com/">         test       </a>    </li>  </ul>


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -