user2900905
user2900905

Reputation: 5

Jquery Foreach After Clicking Button

My site generates html code with php and I need to toggle a section to show after clicking a button. Problem is with this code, after clicking, only reveals one item.

$("body").on('click','.btn',(function(){
document.querySelector(".hide").style.display = "block";

});

I need it to iterate over each .hide

Upvotes: 0

Views: 55

Answers (3)

Emre Karakuz
Emre Karakuz

Reputation: 91

document.querySelectorAll(".hide").forEach( hide => {
  hide.style.display = "block";
})

Upvotes: 0

Kinglish
Kinglish

Reputation: 23654

Seems like this makes more sense. Just remove the hide class?

$("body").on('click', '.btn', (function() {
      $(".hide").removeClass('hide');
    });

Upvotes: 0

Phil
Phil

Reputation: 164798

Just keep using jQuery, specifically the .show() method

Display the matched elements

$(document.body).on("click", ".btn", () => {
  $(".hide").show()
})

Upvotes: 1

Related Questions