Santino
Santino

Reputation: 33

Convert jQuery and vanilla js

I am trying to convert this jQuery code to vanilla js

$(".gone").click(function(){
  $(this).hide();
});

any help?

Upvotes: 1

Views: 72

Answers (1)

Mamun
Mamun

Reputation: 68933

You can try with querySelectorAll() and forEach() and addEventListener()

var gone = document.querySelectorAll(".gone")
Array.from(gone).forEach(function(g){
  g.addEventListener("click", function(){
    this.style.display = "none";
  });
});
<div class="gone">Gone 1</div>
<div class="gone">Gone 2</div>
<div class="gone">Gone 3</div>

Upvotes: 2

Related Questions