Home » Jquery » html – Copy link from same parent div – add to another child within the same div jquery-Exceptionshub

html – Copy link from same parent div – add to another child within the same div jquery-Exceptionshub

Posted by: admin February 24, 2020 Leave a comment

Questions:

I have a cms that outputs lists of categories for ecommerce – I am trying to hide any list items after the first 4 and then add a link to view more which links to a category page. The category page link is within the same parent (or grandparent??) element as the list I am adding it to. I have this working to add the link but it appears to put the first link found across all divs. Please see code below:

$(".ecommMenuItem ul").append('<li><a href="#" class="view-all">View All</a></li>');

  $(".ecommMenuItems").each(function(){
    var divtext = $(".category-link", this).attr("href");
    $(".view-all", this).attr("href", divtext); 
  });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<div class="ecommMenuItems">
	<div class="ecommMenuItem">
		<a class="category-link" href="/subgroup/bga-stencil/" title="BGA Stencil"><i class="category-icon"><img src="/ebuyerfix-com/_img/img/placeholder.jpg"></i><span class="category-title" style="height: 15px;">BGA Stencil</span></a>
		<ul>
			<li><a id="catId8468" href="/_shop/ur-selected-stencil/" title="UR Selected Stencil">UR Selected Stencil</a></li>
			<li><a id="catId8469" href="/_shop/laser-tech-stencil/" title="Laser Tech Stencil">Laser Tech Stencil</a></li>
			<li><a id="catId8470" href="/_shop/3d-stencil/" title="3D Stencil">3D Stencil</a></li>
			<li><a id="catId8471" href="/_shop/black-stencil/" title="Black Stencil">Black Stencil</a></li>
			<li style="display: none;"><a id="catId8472" href="/_shop/magnetic-stencil/" title="Magnetic Stencil">Magnetic Stencil</a></li>
			<li style="display: none;"><a id="catId8473" href="/_shop/macbook-stencil/" title="MacBook Stencil">MacBook Stencil</a></li>
			
		</ul>
	</div>

<div class="ecommMenuItem">
	<a class="category-link" href="/subgroup/hand-tools/" title="Hand Tools"><i class="category-icon"><img src="/ebuyerfix-com/_img/img/placeholder.jpg"></i><span class="category-title" style="height: 15px;">Hand Tools</span></a>
	<ul>
		<li><a id="catId8449" href="/_shop/screwdrivers/" title="Screwdrivers">Screwdrivers</a></li>
		<li><a id="catId8450" href="/_shop/tweezers/" title="Tweezers">Tweezers</a></li>
		<li><a id="catId8451" href="/_shop/tool-kit-set/" title="Tool Kit Set">Tool Kit Set</a></li>
		<li><a id="catId8452" href="/_shop/worksurface-mats/" title="Worksurface Mats">Worksurface Mats</a></li>
		<li style="display: none;"><a id="catId8453" href="/_shop/prying--cutting/" title="Prying &amp; Cutting">Prying &amp; Cutting</a></li>
		<li style="display: none;"><a id="catId8454" href="/_shop/gripping--holding/" title="Gripping &amp; Holding">Gripping &amp; Holding</a></li>
		<li style="display: none;"><a id="catId8455" href="/_shop/crimping--heating/" title="Crimping &amp; Heating">Crimping &amp; Heating</a></li>
		<li style="display: none;"><a id="catId8456" href="/_shop/cleaning-tools/" title="Cleaning Tools">Cleaning Tools</a></li>
		<li style="display: none;"><a id="catId8457" href="/_shop/sim-card-tools/" title="SIM Card Tools">SIM Card Tools</a></li>
		<li style="display: none;"><a id="catId8458" href="/_shop/fume-extractor/" title="Fume Extractor">Fume Extractor</a></li>
		<li style="display: none;"><a id="catId8459" href="/_shop/labour-protection/" title="Labour Protection">Labour Protection</a></li>
		<li style="display: none;"><a id="catId8460" href="/_shop/static-personnel-grounding/" title="Static Personnel Grounding">Static Personnel Grounding</a></li>
		
	</ul>
</div>

</div>

So what I am trying to achieve is that the href from “.category-link” on each individual “.ecommMenuItem” is added to the “.view-all” href in the same .ecommMenuItem.

Any help is much appreciated.

How to&Answer:

You’re looping over the .ecommMenuItems element which is the level above the grouping for each menu. You instead need to loop over the .ecommMenuItem instead:

$(".ecommMenuItem").each(...

Also note that can avoid the need to set an inline display: none on the median li elements by using :nth-child() in CSS. Try this:

$(".ecommMenuItem ul").append('<li><a href="#" class="view-all">View All</a></li>');

$(".ecommMenuItem").each(function() {
  var divtext = $(".category-link", this).attr("href");
  $(".view-all", this).attr("href", divtext);
});
.ecommMenuItem li:nth-child(n+5):not(:last-child) {
  display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<div class="ecommMenuItems">
  <div class="ecommMenuItem">
    <a class="category-link" href="/subgroup/bga-stencil/" title="BGA Stencil"><i class="category-icon">
      <img src="/ebuyerfix-com/_img/img/placeholder.jpg"></i>
      <span class="category-title" style="height: 15px;">BGA Stencil</span>
    </a>
    <ul>
      <li><a id="catId8468" href="/_shop/ur-selected-stencil/" title="UR Selected Stencil">UR Selected Stencil</a></li>
      <li><a id="catId8469" href="/_shop/laser-tech-stencil/" title="Laser Tech Stencil">Laser Tech Stencil</a></li>
      <li><a id="catId8470" href="/_shop/3d-stencil/" title="3D Stencil">3D Stencil</a></li>
      <li><a id="catId8471" href="/_shop/black-stencil/" title="Black Stencil">Black Stencil</a></li>
      <li><a id="catId8472" href="/_shop/magnetic-stencil/" title="Magnetic Stencil">Magnetic Stencil</a></li>
      <li><a id="catId8473" href="/_shop/macbook-stencil/" title="MacBook Stencil">MacBook Stencil</a></li>
    </ul>
  </div>
  <div class="ecommMenuItem">
    <a class="category-link" href="/subgroup/hand-tools/" title="Hand Tools"><i class="category-icon">
      <img src="/ebuyerfix-com/_img/img/placeholder.jpg"></i>
      <span class="category-title" style="height: 15px;">Hand Tools</span>
    </a>
    <ul>
      <li><a id="catId8449" href="/_shop/screwdrivers/" title="Screwdrivers">Screwdrivers</a></li>
      <li><a id="catId8450" href="/_shop/tweezers/" title="Tweezers">Tweezers</a></li>
      <li><a id="catId8451" href="/_shop/tool-kit-set/" title="Tool Kit Set">Tool Kit Set</a></li>
      <li><a id="catId8452" href="/_shop/worksurface-mats/" title="Worksurface Mats">Worksurface Mats</a></li>
      <li><a id="catId8453" href="/_shop/prying--cutting/" title="Prying &amp; Cutting">Prying &amp; Cutting</a></li>
      <li><a id="catId8454" href="/_shop/gripping--holding/" title="Gripping &amp; Holding">Gripping &amp; Holding</a></li>
      <li><a id="catId8455" href="/_shop/crimping--heating/" title="Crimping &amp; Heating">Crimping &amp; Heating</a></li>
      <li><a id="catId8456" href="/_shop/cleaning-tools/" title="Cleaning Tools">Cleaning Tools</a></li>
      <li><a id="catId8457" href="/_shop/sim-card-tools/" title="SIM Card Tools">SIM Card Tools</a></li>
      <li><a id="catId8458" href="/_shop/fume-extractor/" title="Fume Extractor">Fume Extractor</a></li>
      <li><a id="catId8459" href="/_shop/labour-protection/" title="Labour Protection">Labour Protection</a></li>
      <li><a id="catId8460" href="/_shop/static-personnel-grounding/" title="Static Personnel Grounding">Static Personnel Grounding</a></li>
    </ul>
  </div>
</div>