javascript - Add new ID for every new loop -


i'm developing sort of loop using li tags. loop, i'm going use text gonna displayed through simple open , close javascript code i've made when clicking somewhere. guys know, kind of code manipulated through getelementbyid property. however, since i'm going loop these lists items, cannot repeat ids throughout them. so, how can loop these list items without repeating id?

take html below:

<section class="pro-further">   <span>consulte</span><br>   <ul>     <li><div onclick="furtherinfo('test');">info one</div>       <div id="test">text first info</div>     </li>     <li><div>info two</div>       <div>text second info</div>     </li>     <li><div>info three</div>       <div>text third info</div>     </li>     <li><div>info four</div>       <div>text fourth info</div>     </li>     <li><div>info five</div>       <div>text fifth info</div>     </li>     <li><div>info six</div>       <div>text sixth info</div>     </li>   </ul> </section> 

the javascript...

var furtherinfo = function(auei) {   var openinfo = document.getelementbyid(auei);   if(openinfo.style.display == "block") {     openinfo.style.display = "none"   }else {     openinfo.style.display = "block"   } } 

and css

.pro-further ul {   width:100%;   list-style-type:none;   margin-left:0px; }  .pro-further ul li > div {   width:99.3%;   font-size:14px;   color:#fff;   background:url(../images/plus2.png) #23459f no-repeat 99% 50%;   padding:6px 2px 6px 5px;   margin-bottom:3px;   cursor:pointer; }  .pro-further ul li > div + div {   width:98.5%;   font-size:13px;   color:#737373;   background:white;   border:1px solid #23459f;   padding:2px 5px 2px 5px;   margin-top:-3px;   display:none;   cursor:text; } 

thank you!

check here try out

here's id-less solution:

window.onload = function() {     var elems = document.getelementsbytagname("li");     for(var = 0; < elems.length; i++) {         elems[i].onclick = toggle;     } };  function toggle() {     var divs = this.getelementsbytagname("div");     divs[1].style.display = ((divs[1].style.display == "block") ? "none" : "block"); } 

to restrict "getelementsbytagname", i'd suggest giving outer container (the 1 class="pro-further" id, <section id="pro-further-toggles"> specify var elems = document.getelementbyid("pro-further-toggles").getelementsbytagname("li");


Comments

Popular posts from this blog

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

php - render data via PDO::FETCH_FUNC vs loop -

The canvas has been tainted by cross-origin data in chrome only -