Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

106
Views
I Want a Button that hide the div and restore it

I want to create a button that will hide each ticket and one general button that will restore them all.

this is the Code:

return (
  <ul className="tickets">
    {filteredTickets.map((ticket) => (
      <li key={ticket.id} className="ticket">
        <h5 className="headline">{ticket.headline}</h5>
        <p className="text">{ticket.text}</p>
        <footer>
          <div className="data">
            By {ticket.address} | {new Date(ticket.time).toLocaleString()}
          </div>
        </footer>
      </li>
    ))}
  </ul>
);
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

here is an example of what you want! you have to replace myFunction() for your button and myDIV into your element that you want to hide it!

<button onclick="myFunction()">Click Me</button>

    function myFunction() {
      var x = document.getElementById("myDIV");
      if (x.style.display === "none") {
        x.style.display = "block";
      } else {
        x.style.display = "none";
      }
    }

for react = const [visible, setVisible] = useState(true) here is for button <button onlick={() =>setVisible(!visible)}>hide/show

about 4 years ago · Juan Pablo Isaza Report

0

here is a demo in JS, modify to what you want exactly

<ul class="ticket">
<li>
        <p>hey, I'm a P</p>
        <div class="data">I'm a Div</div>
      </li>
</ul>
.hide {display:none}

const generalBtn = document.getElementById(`btn`);
const divContainer = document.querySelector(`.ticket`);
const eachDiv = divContainer.getElementsByClassName(`data`);
generalBtn.addEventListener(`click`, () => {
  [...eachDiv].forEach((div) => {
    div.classList.toggle(`hide`);
  });
});
about 4 years ago · Juan Pablo Isaza Report

0

There is a good solution in your case but as mentioned in the comments, it needs to manipulate the filteredTickets array.

You need to add a property/value to each item of filteredTickets to track or change their state. For example, it can be isVisible property which is a boolean with false or true value.

Now, isVisible value will determine the behavior. let's modify the ticket:

const handleHideTicket = (id) => {
  // find selected ticket and ​change its visibility
 ​const updatedFilterdTickets = filteredTikcets.map(ticket => (ticket.id === id ? {...ticket, isVisible: false} : ticket))
 // now the updatedFilterdTickets need to be set in your state or general state like redux or you need to send it to the server throw a API calling.
}


return (
 ​<ul className="tickets">
   ​{filteredTickets.filter(ticket => ticket.isVisible).map((ticket) => (
     ​<li key={ticket.id} className="ticket">
       ​<h5 className="headline">{ticket.headline}</h5>
       ​<p className="text">{ticket.text}</p>
       ​<footer>
         ​<div className="data">
           ​By {ticket.address} | {new Date(ticket.time).toLocaleString()}
         ​</div>
         // add a button to control visibility of each ticket
         ​<button onClick={() => handleHideTicket (ticket.id)}> click to hid / show </button>
       ​</footer>
     ​</li>
   ​))}
 ​</ul>
);

Explanation:

a new button added to each ticket and pass the handleHideTicket handler to it. If the user clicks on this button, the handler finds that ticket and sets the isVisible property to the false.

On the other hand, we can remove the hidden tickets by applying a simple filter method before map method. so only visible tickets will be displayed.

Now, create a general button to show all the tickets. In this case, you need a handler function that sets all ticket's isVisible value to true

const handleShowAllTickets = () => {
  const updatedFilteredTickets = filteredTickets.map(ticket => ({...ticket, isVisible: true}))
  // now put the updatedFilteredTickets in your general store/post an API call/ update state 
}

Note: as I mentioned in the code's comments, you need to update your filteredTickets array after changing via handlers to reflect the changes in your elements.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!