TransWikia.com

Prevent a button opening a modal if another button has not been pushed first

Stack Overflow Asked by Paul Davis on January 17, 2021

I have a modal that opens with data and another button that lets people see that same data again at a later stage. The only thing is, if the user clicks the button to restore that data, before the data has been applied, the modal opens empty.

So far I have this idea to prevent that button opening an empty modal, but not quite sure how to execute it.

Button withData == Pressed:
  dataPresent= True

if dataPresent === True:
  [what goes here? I want the modal to open as normal]

Button withoutData == Pressed:
  dataNotPresent = True
  dataPresent = False

if dataNotPresent == True:
  [don't open the modal]

I could also add an attribute "disabled" – but again, not quite sure how I put the syntax together (yet).

So the last line could become something like this:

if dataNotPresent == True:
let btn = document.getElementById("myBtn");
    btn.setAttribute("disabled", ""); 

I am very much a beginner here, so this may seem trivial (but not to me). I appreciate your help and I do select right answers too 😉

2 Answers

I'm not sure I understand your problem, so I'm working from the title instead of the description:

  • there are two buttons, A and B
  • pressing button B should open a modal, but only if button A has been pressed first

We just need one variable to track that state.

let buttonA = document.querySelector('button.first') // looks for <button class="first">
let buttonB = document.querySelector('button.second') // looks for <button class="second">
let modal = document.querySelector('.modal') // looks for first element with class="modal"

let buttonAPressed = false

buttonA.on('click', function() { buttonAPressed = true })
buttonB.on('click', function() {
    if(!buttonAPressed) return
    modal.style.display = 'block'
})

Answered by Tom on January 17, 2021

Oh, this was much easier than I anticipated. Because the scenario I had required a function, I just embedded in that function this line:

let currentScenario = document.getElementById("current-scenario-1");
  currentScenario.removeAttribute("disabled");

This works perfectly :)

Answered by Paul Davis on January 17, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP