Sejoo
Sejoo

Reputation: 63

For loop trought id of elements with javascript

#saga1 {
	width: 100%;
	height: 30px;
	background: rgba(255, 132, 132, 0.29);
	border-radius: 10px;
	color: white;
	font-family: verdana;
	text-shadow: 2px 2px 10px black;
	font-size: 18px;
	text-align: center;
	line-height: 30px;
}
#ep1 {
	margin-left: 5px;
	color: white;
	font: normal 16px verdana;
}
#ep1 li {
	list-style: none;
	margin: 5px 0 5px;
	border-top: 0.5px dashed white;
	border-top-right-radius: 5px;
	border-top-left-radius: 5px;
	cursor: pointer;
	color:gray;
	padding: 2px 0 2px;
}
#ep1 li:hover {
	background:rgba(83, 3, 15, 0.29);
	color:white;
	transition: color 0.5s;
}
 <div id="saga1">Bitka Bogova</div>
            <!-- toggle show/hide video on click -->
            <!-- <script src="toggleshowhide.js"></script> -->
             <script></script> // here goes the script 
                <div id="ep1">
                    <ul id="bbogova">
                      <li id="1">Epizoda 1 - Ko će dobiti 100 miliona zenija?</li>
                      <div align="center" class=1></div>
                      <li id=2>Epizoda 2 - Vegeta ide na porodično putovanje?</li>
                        <div align="center" class=2></div>
                      <li id=3>Epizoda 3 - Traži se Super Sajonac Bog!</li>
                      <div align="center" class=3></div>
                      <li id=4>Epizoda 3 - Traže se Zmajeve Kugle!</li>
                      <div align="center" class=4></div>
                      <li id=5>Epizoda 3 - Goku protiv Birusa !</li>
                      <div align="center" class=5></div>
                      <li id="6">Epizoda 6 - Ne ljutite Boga Uništenja!</li>
                       <div align="center" class=6></div>
                      <li id=7>Epizoda 7 - Vegetin razjaren preobražaj!</li>
                      <div align="center" class=7></div>
                      <li id=8>Epizoda 8 - Poslednja šansa od Birusa?!</li>
                      <div align="center" class=8></div>
                      <li id=9>Epizoda 9 - Super Sajonac Bog je rođen!</li>
                      <div align="center" class=9></div>
                      <li id=10>Epizoda 10 - Moć Super Sajonca Boga!</li>
                      <div align="center" class=10></div>
                      <li id=11>Epizoda 11 - Bitka Bogova se nastavlja</li>
                      <div align="center" class=11></div>
                      <li id=12>Epizoda 12 - Bog uništenja protiv SS Boga.</li>
                      <div align="center" class=12></div>
                      <li id=13>Epizoda 13 - Goku, nadmaši moć Super Sajonca Boga!</li>
                      <div align="center" class=13></div>
                      <li id=14>Epizoda 14 - Bitka Bogova se završava!</li>
                      <div align="center" class=14></div>
                    </ul>

I have more then 100 li elements with id's 1,2,3 and so on. Under each element is div with class's 1,2,3 and so on. So i want when i click on any of "li" element to show/hide it's div. I got toggle show hide working but i can't figure it out how to do it with for loop.

I tried:

var click = 1;

for(var i =0;i<=src.length;i++)
    $("#"+i).click(function() {
        if (click == 1)
            $('.'+i).html('<IFRAME SRC="'+src[i]+'" FRAMEBORDER=0 SCROLLING=NO WIDTH=888 HEIGHT=500 allowfullscreen></IFRAME>');
        else if (click == 2) {
            $('.'+i).empty();
            click = 0;
        }

        click++;
    });
}

src is Array full of embed video links

*im sorry, im still learning :D *

Upvotes: 2

Views: 56

Answers (2)

Scott Marcus
Scott Marcus

Reputation: 65873

The ids are irrelevant to the solution and will only complicate matters.

You simply need a way to isolate the li elements that will get clicked and that could be a hierarchical selector or add a class to the parent ul.

Also, note that your IFRAME string has single quotes that are out of place.

var src = ["","","","","",""];

var $list = $(".toggle > li");  // Get a wrapped set of the <li> elements in the target list

// Loop over the li elements
$list.each(function(index, element){

  // Assign a click event handler to each li
  $(element).on("click", function(){
  
     // Get reference to the first div within the li that was clicked
     var $div = $(this).find("div:first-child");
  
     // Toggle the use of the hidden class on the div
     $div.toggleClass("hidden");
     
     // If the div is hidden...
     if($div.hasClass("hidden")){
       $div.html("");  // Clear the contents
     } else {
       // Populate with an iframe that relies on the index of the parent li
       $div.html("<IFRAME SRC='" + src[index] + "' FRAMEBORDER='0' SCROLLING='NO' WIDTH='888' HEIGHT='500' allowfullscreen></IFRAME>)");
     }
  });
});
.hidden { display:none; }

iframe { background-color:yellow; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- Giving the parent of the li's that will be clicked a class will allow you
     to gather them up into a collection easily. -->
<ul class="toggle">
 <li>item
   <div class="hidden">surprise!</div>
 </li>
 <li>item
   <div class="hidden">surprise!</div>
 </li>
 <li>item
   <div class="hidden">surprise!</div>
 </li>
 <li>item
   <div class="hidden">surprise!</div>
 </li>
 <li>item
   <div class="hidden">surprise!</div>
 </li>
 <li>item
   <div class="hidden">surprise!</div>
 </li> 
</ul>

Upvotes: 2

Kamran Hashmi
Kamran Hashmi

Reputation: 61

use your li id in onclick="function(id)" and define a variable in your javascript function to catch this id. then use that variable wherever you want it in that function it will only change that id's property.

Upvotes: 0

Related Questions