jquery - prependTo item only once on div -


let's have data , want prependto once, because when tried prependto $('.entry-title') ('.container) display twice every article.

<article class="blogpage-posts">   <div class="container">     <a href="#">data one</a>     <h1 class="entry-title">data prepend one</h1>   </div> </article>  <article class="blogpage-posts">   <div class="container">     <a href="#">data two</a>     <h1 class="entry-title">data prepend two</h1>   </div> </article> 

this code...

$(document).ready(function(){      $('.entry-title').prependto('.container'); }); 

this result when using code.

data prepend 1 data prepend 2 <--- data display twice data 1  data prepend 2 data prepend 1 <--- data display twice data 1 2 

use code:

$(document).ready(function(){       $('.entry-title').each(function(){          $(this).prependto($(this).closest('.container'));       });  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <article class="blogpage-posts">    <div class="container">      <a href="#">data one</a>      <h1 class="entry-title">data prepend one</h1>    </div>  </article>    <article class="blogpage-posts">    <div class="container">      <a href="#">data two</a>      <h1 class="entry-title">data prepend two</h1>    </div>  </article>


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -