Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <main>
  <input type="text" class="message-input" placeholder="Enter Message" required>
  <button class="send-btn">Send</button>
  <div class="user user1">
    <div>
      <h2 class="userItem">User1</h2> <button class="user1-subscribe">Subscribe</button> <button class="user1-unsubscribe">Unsubscribe</button>
    </div>
    <ul class="user1-messages">
    </ul>
  </div>

  <div class="user user2">
    <div>
      <h2 class="userItem">User2</h2> <button class="user2-subscribe">Subscribe</button> <button class="user2-unsubscribe">Unsubscribe</button>
    </div>
    <ul class="user2-messages">
    </ul>
  </div>

  <div class="user user3">
    <div>
      <h2 class="userItem">User3</h2> <button class="user3-subscribe">Subscribe</button> <button class="user3-unsubscribe">Unsubscribe</button>
    </div>
    <ul class="user3-messages">
    </ul>
  </div>
</main>
              
            
!

CSS

              
                * {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}
main {
  padding: 1rem;
}
button {
  cursor: pointer;
  padding: 8px 15px;
  display: inline-block;
  margin: 0 10px;
}
input {
  padding: 8px 15px;
  width: 320px;
}
input,
.user {
  margin: 10px 0;
}

.user div{
  display: flex;
}
              
            
!

JS

              
                interface NotificationObserver {
  onMessage(message: Message): string;
}

interface Notify {
  sendMessage(message: Message): any;
}

class Message {
  message: string;

  constructor(message: string) {
    this.message = message;
  }

  getMessage(): string {
    return `${this.message} from publication`;
  }
}

class User implements NotificationObserver {
  element: Element;

  constructor(element: Element) {
    this.element = element;
  }

  onMessage(message: Message) {
    return (this.element.innerHTML += `<li>you have a new message - ${message.getMessage()}</li>`);
  }
}

class MailingList implements Notify {
  protected observers: User[] = [];

  notify(message: Message) {
    this.observers.forEach((observer) => {
      observer.onMessage(message);
    });
  }

  subscribe(observer: User) {
    this.observers.push(observer);
  }
  unsubscribe(observer: User) {
    this.observers = this.observers.filter(
      (subscriber) => subscriber !== observer
    );
  }

  sendMessage(message: Message) {
    this.notify(message);
  }
}

const messageInput: Element = document.querySelector(".message-input");

const user1: Element = document.querySelector(".user1-messages");
const user2: Element = document.querySelector(".user2-messages");
const user3: Element = document.querySelector(".user3-messages");

const u1 = new User(user1);
const u2 = new User(user2);
const u3 = new User(user3);

const subscribeU1: Element = document.querySelector(".user1-subscribe");
const subscribeU2: Element = document.querySelector(".user2-subscribe");
const subscribeU3: Element = document.querySelector(".user3-subscribe");

const unSubscribeU1: Element = document.querySelector(".user1-unsubscribe");
const unSubscribeU2: Element = document.querySelector(".user2-unsubscribe");
const unSubscribeU3: Element = document.querySelector(".user3-unsubscribe");

const sendBtn: Element = document.querySelector(".send-btn");

const mailingList = new MailingList();

mailingList.subscribe(u1);
mailingList.subscribe(u2);
mailingList.subscribe(u3);

subscribeU1.addEventListener("click", () => {
  mailingList.subscribe(u1);
});
subscribeU2.addEventListener("click", () => {
  mailingList.subscribe(u2);
});
subscribeU3.addEventListener("click", () => {
  mailingList.subscribe(u3);
});

unSubscribeU1.addEventListener("click", () => {
  mailingList.unsubscribe(u1);
});
unSubscribeU2.addEventListener("click", () => {
  mailingList.unsubscribe(u2);
});
unSubscribeU3.addEventListener("click", () => {
  mailingList.unsubscribe(u3);
});

sendBtn.addEventListener("click", () => {
  mailingList.sendMessage(new Message(messageInput.value));
});

              
            
!
999px

Console