Close a window with JavaScript onClick

Simpleacc

On my website I have 4 Windows (identified by target1, target2, target3 and target4). On these Windows I want to add a button with a name of "close" and using an onClick event call the correct function depending on which window.

function close_window1(){
    document.getElementById('target1').style.display = 'none';  
}

function close_window2(){
    document.getElementById('target2').style.display = 'none';  
}

function close_window3(){
    document.getElementById('target3').style.display = 'none';  
}

function close_window4(){
    document.getElementById('target4').style.display = 'none';  
}

I'm sure there is a better way than this:

function close_window(){
    $(this).parents('div').style.display='none'
}

This will close only the window that has the event, but it doesn't work.

Could someone help me please?

Michael Schneider

if i understand that correctly you can simply use this:

var windows = $('#target1, #target2, #target3, #target4');
windows.on('click', function() {
  $(this).hide();
});

You should consider giving the button an class:

HTML

<div class="windows">
 <button class="close_window">X</button>
</div>

JS

var closeButtons = $('.close_window');
closeButtons.on('click', function() {
  $(this).parent().hide();
});

Then you can get actually the goal you want :-)

Regards

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Close a window with JavaScript onClick

From Dev

Javascript dialog window close

From Dev

WPF C# Close Window onclick

From Dev

Atom Electron - Close the window with javascript

From Dev

Javascript to automatically close a popup window

From Dev

onclick in window.onload in javascript

From Dev

How to close the browser tab(parent window) with JavaScript

From Dev

Cannot close dialog window using Javascript in Crm

From Dev

Blazor Emit Javascript to Close Browser Window

From Dev

How to close a popup window using Javascript

From Dev

How close popup window on ESC keydown on javascript

From Dev

Run PHP code on Window / Tab close with Javascript

From Dev

Javascript - open window on one page, then close it on another

From Dev

Debugger not stepping through window.close in Javascript

From Dev

How to call parent window function on close of child window in javascript?

From Dev

How to open a window and close another window when a button is pressed with javascript?

From Dev

Is it possible in Javascript to close the window that was just opened by window.open?

From Dev

Javascript window.location.href onClick not working

From Dev

opening a fixed window on 'onclick' event - javascript & html

From Dev

Javascript onclick window.location for page anchors?

From Dev

Javascript onclick window.open doesnt work

From Dev

Javascript window.location.href onClick not working

From Dev

Javascript - window.close, self.close, parent.close, none works to close window, tab in Google Chrome

From Dev

Javascript reveal text onclick and then close all on next click

From Dev

How to automatically close the new popup window in Javascript and PHP?

From Dev

Handling Window.close in JavaScript through UIWebView - Obj C

From Dev

Atom Electron Close and Minimize window using the html button and javascript

From Dev

Wait for modal window to close and then execute the following lines in javascript

From Dev

JavaScript: find pop-up window by URL and close it

Related Related

  1. 1

    Close a window with JavaScript onClick

  2. 2

    Javascript dialog window close

  3. 3

    WPF C# Close Window onclick

  4. 4

    Atom Electron - Close the window with javascript

  5. 5

    Javascript to automatically close a popup window

  6. 6

    onclick in window.onload in javascript

  7. 7

    How to close the browser tab(parent window) with JavaScript

  8. 8

    Cannot close dialog window using Javascript in Crm

  9. 9

    Blazor Emit Javascript to Close Browser Window

  10. 10

    How to close a popup window using Javascript

  11. 11

    How close popup window on ESC keydown on javascript

  12. 12

    Run PHP code on Window / Tab close with Javascript

  13. 13

    Javascript - open window on one page, then close it on another

  14. 14

    Debugger not stepping through window.close in Javascript

  15. 15

    How to call parent window function on close of child window in javascript?

  16. 16

    How to open a window and close another window when a button is pressed with javascript?

  17. 17

    Is it possible in Javascript to close the window that was just opened by window.open?

  18. 18

    Javascript window.location.href onClick not working

  19. 19

    opening a fixed window on 'onclick' event - javascript & html

  20. 20

    Javascript onclick window.location for page anchors?

  21. 21

    Javascript onclick window.open doesnt work

  22. 22

    Javascript window.location.href onClick not working

  23. 23

    Javascript - window.close, self.close, parent.close, none works to close window, tab in Google Chrome

  24. 24

    Javascript reveal text onclick and then close all on next click

  25. 25

    How to automatically close the new popup window in Javascript and PHP?

  26. 26

    Handling Window.close in JavaScript through UIWebView - Obj C

  27. 27

    Atom Electron Close and Minimize window using the html button and javascript

  28. 28

    Wait for modal window to close and then execute the following lines in javascript

  29. 29

    JavaScript: find pop-up window by URL and close it

HotTag

Archive