Home > database >  Get first div after button is clicked
Get first div after button is clicked

Time:06-05

I have the following bit about shop info:

<h3 ><shop>Shop name</shop></h3>
<button type="button" id="banff" onclick="showShop(this.id)"><shop-title><b>Bakery Shop</b></shop-title></button>
<p ><shop-info>Shop Address · Shop number</shop-info></p>
  <div id="shop"  >
    <p><shop-info>Opening soon</shop-info></p>
  </div>

What I'm trying to do is that once the button is clicked, it will get the first div after the button and toggle the hidden class without referencing the id, there will be multiple shops so I am trying to create one function which will work for all of them rather than create an individual function for each.

I thought I could do something like:

function showShop(id) {
const elem = document.getElementById(id);
 alert(id);
const div = elem.closest('div');
div.classList.toggle('hidden'); 

}

But it's referencing the first div on the page rather than the first one after the button, where have I gone wrong here? Or do I need to go about this a different way?

Thanks in advance for any advice

CodePudding user response:

What about connecting the button id with the div id?

something like:

  <h3 ><shop>Shop name</shop></h3>
  <button type="button" id="button-1" onclick="showShop(this.id)">
    <shop-title><b>Bakery Shop</b></shop-title>
  </button>
  <p ><shop-info>Shop Address · Shop number</shop-info></p>
  <div id="shop-button-1" >
   <p><shop-info>Opening soon</shop-info></p>
  </div>

and in the javascript code would be possible to simplify to:

function showShop(id) {
  const elem = document.getElementById(id);
  const div = document.getElementById('shop-'   id);

  div.classList.toggle('hidden'); 
}

CodePudding user response:

You should try and avoid using ids as they are difficult to keep track of. Instead you should navigate relative to the clicked button. Below is a script that can easily take care of multiple shop sections without using any ids:

document.querySelectorAll("button").forEach(btn=>{
  for(var div=btn;div=div.nextElementSibling;)
    if(div.tagName==="DIV") break;
  if(div) btn.onclick=()=>div.classList.toggle("hidden")
})
.hidden {display:none}
<h3 ><shop>Shop name</shop></h3>
<button><shop-title><b>Bakery Shop</b></shop-title></button>
<p ><shop-info>Shop Address · Shop number</shop-info></p>
  <div >Info on bakery shop:
<p><shop-info>Opening soon</shop-info></p>
  </div>
<button><shop-title><b>Iron Monger</b></shop-title></button>
<p ><shop-info>Shop Address · Shop number</shop-info></p>
  <div >Info on iron monger's:
<p><shop-info>already open!</shop-info></p>
  </div>
<button><shop-title><b>Fish Monger</b></shop-title></button>
<p ><shop-info>Shop Address · Shop number</shop-info></p>
  <div >Info on fish monger's:
<p><shop-info>will never open!</shop-info></p>
  </div>

In my latest update I tweaked the snippet again so that now I do the "relative navigation to the associated <div>" only once: at the time when I define and add the onclick eventlistener.

  • Related