1

I'm very new to JavaScript, and web development overall and really can't grasp why my functions aren't invoked correctly.

I'm trying to create a "controller"-class as I would have called it, which contain functions such as open() and close() for a popup element.

Here is the controller class:

function PopupController(containerElementId){
 _containerElement = document.getElementById(containerElementId);
 _contentElement = _containerElement.getElementsByClassName("modal-body")[0];
 window.onclick = function(event) {
 if (event.target !== _containerElement && _containerElement.style.display === "block") {
 _containerElement.style.display = "none";
 }
 }
}
PopupController.prototype = {
 constructor: PopupController
}
PopupController.prototype.open = function(contentHtml) {
 _contentElement.innerHTML = contentHtml;
 _containerElement.style.display = "block";
}
PopupController.prototype.close = function() {
 _containerElement.style.display = "none";
 _contentElement.innerHTML = "";
}

And then we have the controller object with global scope which is, resides within the head of the HTML.

this._popupController = new PopupController("popupContainerId");
function openPopup(){
 this._popupController.open("<p>testing</p>");
}

And the HTML.

<body>
 <button type="button" class="btn btn-info btn-lg" data-toggle="modal" onclick="openPopup()">Read more</button>
 <div class="popupContainer" tabindex="-1" role="dialog" id="popupContainerId">
 <div class="modal-dialog" role="document">
 <div class="modal-content">
 <div class="modal-header">
 <button type="button" class="close" aria-label="Close"><span aria-hidden="true">&times;</span></button>
 <h4 class="modal-title">Modal title</h4>
 </div>
 <div class="modal-body">
 </div>
 <div class="modal-footer">
 <button type="button" class="close">Close</button>
 </div>
 </div>
 </div>
 </div>
</body>

When I debug this in Chrome, it feels like the open() is invoked at page load, though I think I've wired it to be invoked on the button click. The popup does not get displayed anyhow, but that's another issue.

Hope anyone can give me a tip on how to proceed?

Cerbrus
73.3k19 gold badges138 silver badges151 bronze badges
asked Dec 9, 2016 at 10:17

2 Answers 2

1

You are initializing the controller in the HEAD tag?

this._popupController = new PopupController("popupContainerId");

Because for me, Firefox is complaining that "popupContainerId" element does not exists (yet). JS is run before whole HTML is loaded. But can be unrelated.

answered Dec 9, 2016 at 10:29
Sign up to request clarification or add additional context in comments.

3 Comments

<head><script>Initialization happens here</script></head>. Thanks for the tip, I will get back in a minute!
Now I added the the initialization inside a function which is invoked on page load instead so it does not return undefined anymore.
After this it worked a lot better... Though the popup just get displayed for a millisecond or something. But that's another question that I will post now! :) Thanks.
0

A little upgrade on your code

  • better to prefix class attributes with this.
  • no need to declare 'constructor', the main function is already set to be the constructor
  • in constructor need to save a reference to 'this' in the onclick function

function PopupController(containerElementId){
 this._containerElement = document.getElementById(containerElementId);
 this._contentElement = this._containerElement.getElementsByClassName("modal-body")[0];
 var that = this;
 window.onclick = function(event) {
 if (event.target !== that._containerElement && that._containerElement.style.display === "block") {
 that._containerElement.style.display = "none";
 }
 }
}

https://jsfiddle.net/m1zq7otb/

answered Dec 9, 2016 at 10:57

2 Comments

I thought I had to add the constructor to prototype if I wanted more popup controllers on one page! But I most likely missread it :) Thanks for the clean up!
@Alex: Set the popup's default display to none, with css. That way, it's hidden on page load.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.