Marwane Bettach Marwane Bettach - 1 month ago 11
CSS Question

jquery hide element if href is equal to

I recently made a jquery code that should hides an element if it's href is equal to another element's but i can't be able to make it work...

jsfiddle

HTML

<div class="a">
<a href="1">
<img scr="a">
</a>
</div>

<div class="thumb">
<a href="1">
<img scr="a">
</a>
</div>

<div class="thumb b">
<a href="2">
<img scr="b">
</a>
</div>


CSS

.a{width:400px;height:100px;background-color:black;}
.thumb{width:400px;height:100px;background-color:green;}
.b{background-color:yellow;}


JQUERY

var mainhref = $(".a a").attr('href');

if($("a", this).attr('href') == mainhref ) {
$(".a").hide();
}
else {
$(".a").show
}

Answer

Using plain javascript :D

let ar = document.getElementsByTagName('a'),
    holdarray = [];

Array.from(ar, elem => {
  if(holdarray.includes(elem.getAttribute('href')))
    elem.parentNode.style.display = 'none'
  else
    holdarray.push(elem.getAttribute('href'))
})
.a { width:400px;height:100px;background-color:black; }
.thumb { width:400px;height:100px;background-color:green; }
.b { background-color:yellow; }
<div class="a" >
  <a href="1"></a>
</div>

<div class="thumb">
  <a href="1"></a>
</div>

<div class="thumb b">
  <a href="2"></a>
</div>

Comments