1

I have a bootstrap modal that has a form. I need to be able to click outside the modal so that the modal closes when the form is submitted. how can i achieve this in react, thanks

EDIT:

bootstrap_modal
   form_inside_bootstrap_modal
      submit_button_inside_form
   </ form_inside_bootstrap_modal>
</ bootstrap_modal>

I want to be able to close the bootstrap modal when submit_button_inside_form is clicked

SOLUTION:

document.elementFromPoint(x,y).click()
Kritish Bhattarai
  • 1,501
  • 15
  • 20

2 Answers2

1

You can set eventListener on click in componentDidMount.

like this:

  useEffect(() => {
    window.addEventListener("click", function(e) {
      if (document.getElementById("modalWindow").contains(e.target)) {
        alert("clicked inside");
      } else {
        alert("clicked outside");
      }
    });
    return () => window.removeEventListener("click");
  }, []);

see the example i wrote here.

Vahid Alimohamadi
  • 4,900
  • 2
  • 22
  • 37
0

It's simulating a click at a position: x,y coordinates with javascript which can be achieved using this

document.elementFromPoint(x, y).click();

Kritish Bhattarai
  • 1,501
  • 15
  • 20