javascript - How to fadeIn and fadeOut individual Div elements created with foreach? -


i have foreach loop , creates multiple div elements.

i added onclick event when user clicks on div doing fadein or fadeout.

the problem is, of div's obvouisly have same id , when click on 1 of div's fade in , fade out first div element.

i don't know how fade in , fade out them individually.

i have added code in fiddle

  function showitems() {      var x = document.getelementbyid('showitemdiv');      if (x.style.display === 'none') {        x.style.display = 'block';      } else {        x.style.display = 'none';      }    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <div class="row rounded-top" style="margin-top:5px;background-color:lightblue;padding:5px;" onclick="showitems()">    <div class="col-lg-6 col-md-12 col-sm-12 col-xs-12">      <div class="row">        <span class="" style="font-size:10px;">item name:</span>      </div>      <div class="row">        <p>          <span class="" id="">item 1</span>        </p>      </div>    </div>      <div id="showitemdiv" style="display: none;">      <div class="row" style="margin-top:15px;background-color: #ffe4e1;padding:5px;">        <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2">            <div class="row">            <span class="" style="font-size:10px;">time setup:</span>          </div>            <div class="row">            <p>              <span class="" id="id">1</span>            </p>          </div>          </div>      </div>    </div>  </div>    <div class="row rounded-top" style="margin-top:5px;background-color:lightblue;padding:5px;" onclick="showitems()">    <div class="col-lg-6 col-md-12 col-sm-12 col-xs-12">      <div class="row">        <span class="" style="font-size:10px;">item name:</span>      </div>      <div class="row">        <p>          <span class="" id="">item 2</span>        </p>      </div>    </div>      <div id="showitemdiv" style="display: none;">      <div class="row" style="margin-top:15px;background-color: #ffe4e1;padding:5px;">        <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2">            <div class="row">            <span class="" style="font-size:10px;">time setup:</span>          </div>            <div class="row">            <p>              <span class="" id="id">2</span>            </p>          </div>          </div>      </div>    </div>  </div>

you should not use same id multiple elements. id each element should unique. if use same id selector returns first element matching id. should use class same name.

for each element clicked can find parent , can find child required class , toggle visibility.

  function showitems(ev) {      var x = $(ev.target).parents('.row.rounded-top').find('.showitemdiv');      $(x).toggle();  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <div class="row rounded-top" style="margin-top:5px;background-color:lightblue;padding:5px;" onclick="showitems(event)">    <div class="col-lg-6 col-md-12 col-sm-12 col-xs-12">      <div class="row">        <span class="" style="font-size:10px;">item name:</span>      </div>      <div class="row">        <p>          <span class="" id="">item 1</span>        </p>      </div>    </div>      <div class="showitemdiv" style="display: none;">      <div class="row" style="margin-top:15px;background-color: #ffe4e1;padding:5px;">        <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2">            <div class="row">            <span class="" style="font-size:10px;">time setup:</span>          </div>            <div class="row">            <p>              <span class="" id="id">1</span>            </p>          </div>          </div>      </div>    </div>  </div>    <div class="row rounded-top" style="margin-top:5px;background-color:lightblue;padding:5px;" onclick="showitems(event)">    <div class="col-lg-6 col-md-12 col-sm-12 col-xs-12">      <div class="row">        <span class="" style="font-size:10px;">item name:</span>      </div>      <div class="row">        <p>          <span class="" id="">item 2</span>        </p>      </div>    </div>      <div class="showitemdiv" style="display: none;">      <div class="row" style="margin-top:15px;background-color: #ffe4e1;padding:5px;">        <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2">            <div class="row">            <span class="" style="font-size:10px;">time setup:</span>          </div>            <div class="row">            <p>              <span class="" id="id">2</span>            </p>          </div>          </div>      </div>    </div>  </div>


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -