How can you enable a button to be clicked from within a drawer?

Hey there, I've got a div that toggles hide/show like a drawer when clicked. When the drawer is in show state, it contains a button. How can I make the button clickable without toggling the drawer? Any ideas on this?! `zIndex` doesn't seem to be working properly.

<div onClick={this.toggleDiv.bind(this, "divid")} >
    <div>
        <div className="hide" id="divid" style={{zIndex: "1"}}>
            <br />
            <button  onClick={this.divRemainShowAndAlertHi(this, plateInfo)}>
                click me
            </button>
            <br/>
        </div>
    </div>
</div>


divRemainsShowAndAlertHi(){
    alert('Hi button click is working but drawer does not slide out')
}


toggleDiv(id){
    // logic for hide/show functionality
    const plateclass = document.getElementById(id);
    const changedclass = plateclass.className == 'show' ? 'hide' : 'show';
    plateclass.setAttribute('class', changedclass);
}

Answer №1

If you want to prevent the click event from bubbling up when the button is clicked, you need to use stopPropagation. Also, make sure you include bind in the onClick function like this:

onClick={this.divRemainShowAndAlertHi.bind(this, plateInfo)}

<div onClick={this.toggleDiv.bind(this, "divid")} >
   <div >
    <div className="hide" id="divid" style={{zIndex: "1"}}>
      <br />
      <button  onClick={this.divRemainShowAndAlertHi.bind(this, plateInfo)}>click me</button>
      <br/>
     </div>

 </div>
 </div>


 divRemainsShowAndAlertHi(info, e){
   e.stopPropagation();
   alert('Hi button click is working but drawer does not slides')
}

It's also important to follow React practices and avoid direct DOM manipulation by using state instead.

state = {
    visibleModel: ''
}

// In render
<div onClick={this.toggleDiv.bind(this, "divid")} >
   <div >
    <div className={this.state.modelState === "divid" ? "show": "hide"} id="divid" style={{zIndex: "1"}}>
      <br />
      <button  onClick={this.divRemainShowAndAlertHi.bind(this, plateInfo)}>click me</button>
      <br/>
     </div>

  </div>
 </div>

// Function implementation
toggleDiv(id){
    this.setState({visibleModel: id});
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Could you explain the contrast among "yarn serve," "yarn start," and "yarn build"?

Although both "yarn serve" and "yarn start" can launch my Vue project, I'm unsure of the differences between them. I've heard that "yarn build" is for packaging, but why don't I use it at work? Usually, I just upload my code to git and let ...

What is the best way to add styling to my image when hovered over within a link on a responsive website?

I'm having trouble aligning the :hover state with the original footer image links. Here is an edited version of the flicker issue I encountered on Chrome, while nothing appeared on Safari. https://i.sstatic.net/ExdPW.png I apologize for linking to ...

Tips for organizing AJAX code to show images and videos in HTML with a switch case approach

I have 2 tables, one for images and one for videos. Within my HTML code, I have three buttons: slide_image, video, and single_image. Using iframes, I am able to load images and videos on the same page. When I click on the slide_image button, it displays im ...

Create a duplicate of the table and remove specific rows

Hi there, I have a query about duplicating a table. I am looking to extract specific results and display only those results. To illustrate, here is an example in HTML code: <table class="table table-striped" id="ProfileList2"> <tbody> ...

Display data fetched from concurrent Ajax calls in a React Component

I am currently in the process of converting my original project to Reactjs. Since I am new to this and it's my first application, I could use some guidance on how to effectively implement this in a React-friendly way and enhance both my skills and the ...

What is the proper way to specify the background image path in CSS?

My CSS file is located at: Project/Web/Support/Styles/file.css The image I want to use is found here: Project/Web/images/image.png I am attempting to include this image in my CSS file. I have tried the following methods: 1) background-image: url(/images ...

Discovering old (potentially neglected) npm dependencies: strategies for locating outdated packages

I am aware of the npm outdated command, which shows out-of-date dependencies only if a new version has been published. However, in certain cases (such as abandoned projects), there may not be a new version released and the package could still be considere ...

Automated downloading based on operating system recognition

1067/5000 How can I use JavaScript to automatically determine the user's operating system on a webpage and then download the correct installation file? Here is the code I have: HTML <!DOCTYPE html> <html> <body> <iframe id=" ...

Ways to ensure that an inner div causes the outer div to shift downward

Imagine I have a div. Within this div, there are two divs positioned side by side with the float left property. The div on the left is designated for an image. However, if the image is too tall, it does not expand the outer div to accommodate it. Instead ...

Is it possible to nest v-for directives within a component file?

Even after going through the VueJS tutorials multiple times, I am still unable to find a solution to this problem. My issue revolves around displaying a list of lists using accordions, which is supposed to work smoothly with vue-strap components. For exa ...

Error: JSON data contains an unexpected token '<', which is causing a SyntaxError and making the JSON invalid

This PERN application is throwing an error that I have never encountered before, and after searching on Google, I couldn't find any relevant information. Upon reviewing the index.html file, everything seems to be in order: <!DOCTYPE html> <h ...

Understanding the Parameters for discord.js Slash Commands

I am currently working on a calculation that involves using parameters input through a slash command. While entering the parameters works without any issues, I am facing difficulty in retrieving them. The current code is resulting in an error TypeError: ...

What is the correct method for replacing a static page with an "outdated page"?

My website features several static promotional pages. However, I am facing the challenge of displaying an expired page once a promotion has passed. How can I achieve this seamlessly with static pages in NextJS? On my first attempt, I checked for expiratio ...

Tips for concealing the cursor indefinitely in Typed JS

Is there a way to hide the cursor permanently in TypedJS so that it doesn't blink or show while typing? I've come across tutorials on hiding it after typing is complete, but not from the beginning. Any suggestions on how to achieve this? ...

Why isn't Google Map showing up in my HTML <div> section?

I am currently working on a web page that consists of two containers. The mainmapdiv container covers the entire page, while the mainhomediv container is positioned on top of the mainmapdiv. My goal is to hide the mainhomediv container and show a Google Ma ...

Programming on the customer side

Are there any recommendations for a book published in 2015 that covers both Angular 2 and React? Also, are there any free online courses on Coursera or other platforms that cover these topics? I want to gain a good understanding of both technologies so tha ...

Tips for transforming a menu into a dropdown menu

Is there a way to convert the current menu into a Dropdown menu? Currently, the menu is not collapsing and it keeps opening. Any suggestions on how to achieve this? Here is an example for reference: https://i.stack.imgur.com/2X8jT.png ar ...

Using SetTimeout with the TextInput component in a React-Native application

Currently, I am working on creating a SearchBar component for my new Android application using React-Native. As a newcomer to React-Native, I created a function called _changeInput() to handle passing text to a local function. Initially, the text pass wor ...

Leverage angular-translate to establish placeholder text upon blurring

Just starting out with Angular and facing the challenge of implementing localization in my project. I have a lot of input fields that need their placeholders translated. In my HTML, I'm trying to achieve this: <input type="email" placeholder="{{ & ...

Tips for integrating Twitter sharing functionality in React JS

I am looking for a way to enable users to easily share images from my website on Twitter. Although I tried using the react-share module, it does not provide an option to directly share images. This is the snippet of code I currently have: import { Sh ...