Jump to content
Larry Ullman's Book Forums

...onclick =Null;


Recommended Posts

Hi,

 

I have a question about Creating Modal Windows example... Quote:

 

 

function openModal() {
    'use strict';
    document.getElementById('closeModal').onclick = closeModal;
    document.getElementById('modal').style.display = 'inline-block';
    document.getElementById('openModal').onclick = null;
}

function closeModal() {
    'use strict';
    document.getElementById('openModal').onclick = openModal;
    document.getElementById('modal').style.display = 'none';
    document.getElementById('closeModal').onclick = null;
}

window.onload = function() {
    'use strict';
    document.getElementById('openModal').onclick = openModal;
};

 

If I remove the following lines (altogether, all three lines):

  • In function openModal(): document.getElementById('openModal').onclick = null;
  • In function closeModal(): document.getElementById('openModal').onclick = openModal;
  • In function closeModal(): document.getElementById('closeModal').onclick = null;

so that the new code looks like this:

 

 

function openModal() {
    'use strict';
    document.getElementById('closeModal').onclick = closeModal;
    document.getElementById('modal').style.display = 'inline-block';
}

function closeModal() {
    'use strict';
    document.getElementById('modal').style.display = 'none';
}

window.onload = function() {
    'use strict';
    document.getElementById('openModal').onclick = openModal;
};

 

everything works well, there are no errors. I don't understand why there are these three lines?

 

Thank you in advance!

Link to comment
Share on other sites

The point of those lines is to drop event handlers when they're not needed. This is a tidiness issue, that's not required, and won't affect the functionality, but is arguably more professional and can prevent memory leaks.

  • Upvote 1
Link to comment
Share on other sites

 Share

×
×
  • Create New...