Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8507401
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T02:51:41+00:00 2026-06-11T02:51:41+00:00

I am writing a small webapp and I decided I will not use any

  • 0

I am writing a small webapp and I decided I will not use any framework. I will write only vanilla javascript.

The problem I have is that I want to be able to open three different types of modal windows with different keyboard keys. For example, g will open one type, d will open another, and m yet another.

I tried to implement the g one, which will contain a title and an input form, and the d one, with a title and two buttons. Here is my code so far:

var width = 720;
var height = 350;
var modal = document.createElement('div');
modal.className  = "modal";
modal.style.width = width;
modal.style.height = height;
modal.style.position = 'absolute';
modal.style.top = (window.innerHeight - parseInt(modal.style.height)) / 2 +'px';
modal.style.left = (window.innerWidth - parseInt(modal.style.width)) / 2 +'px';

var ngroup = modal;
var title = document.createElement('span');
title.innerHTML = "TYPE GROUP NAME"
var input = document.createElement('input');
input.type = "text";
ngroup.appendChild(title);
ngroup.appendChild(input);

var del = modal;
var dtitle = document.createElement('span');
dtitle.innerHTML = "DO YO WANT TO DELETE SELECTED COLOR(S)"
var dinput = document.createElement('input');
dinput.type = "button";
dinput.value = "yes";
var dinput1 = document.createElement('input');
dinput1.type = "button";
dinput1.value = "no";
del.appendChild(dtitle);
del.appendChild(dinput);
del.appendChild(dinput1);   
function openModal(type) {
  var body = document.getElementsByTagName('body')[0];
  body.appendChild(type);
}
function closeModal() {
  document.body.removeChild(modal);
}

and this is modal.js, which is included in the main page and called by this:

document.addEventListener('keydown', function(event) {
    if(event.keyCode == 71) {
        openModal(del);
    }
    else if(event.keyCode == 39) {
        closeModal();
    }
});

When I hit g, it opens a modal with every element: two titles, one text field and two buttons. Why? What is the problem with my Javascript? I know it’s still not organized, but I’d like to make things work before getting into that.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T02:51:42+00:00Added an answer on June 11, 2026 at 2:51 am

    Even if you aren’t using a library, I would recommend using the same tactic as jQuery. Specifically, I would specify all three dialogs in the static HTML in a hidden div. For example, the following would be the ‘d’ dialog:

    <div id="d_dialog" style="display:none" class="dialog">
        <span>DO YO WANT TO DELETE SELECTED COLOR(S)</span>
        <input type="button" value="yes"/>
        <input type="button" value="no"/>
    </div>
    

    Then, from JavaScript when you want to open the dialog, just show the hidden div.

    document.getElementById('d_dialog').style.display = '';
    

    This will make your work much more maintainable than creating all the HTML elements from JavaScript. You can specify the style of the dialog by adding a class called dialog to your CSS.

    The specific problem with your code

    The specific problem with your code is you are reassigning the same variable modal to multiple other variables.

    var del = modal;
    

    This doesn’t make a copy of the modal element. It just makes the new variable del reference the same object as the modal variable. Therefore when you call:

    del.appendChild(dtitle);
    

    It behaves the same as if you had called

    modal.appendChild(dtitle);
    

    So all the controls are added to the modal element. And when you show the dialog with

    opendDialog(del);
    

    It behaves as if you had called

    openDialog(modal);
    

    And shows the element with all the controls on it. To make your code work, you should create a function createModal that just does the following:

    function createModal()
    {
        var width = 720;
        var height = 350;
        var modal = document.createElement('div');
        modal.className  = "modal";
        modal.style.width = width;
        modal.style.height = height;
        modal.style.position = 'absolute';
        modal.style.top = (window.innerHeight - parseInt(modal.style.height)) / 2 +'px';
        modal.style.left = (window.innerWidth - parseInt(modal.style.width)) / 2 +'px';
        return modal;
    }
    

    Then, for each of the dialogs, instead of

    var del = modal;
    

    Do

    var del = createModal();
    

    This will create a new element instead of creating a new variable that refers to the same element.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I intend on writing a small webapp using the Play framework with Scala and
I'm writing small php extension and have problem when building it. The code: PHP_RINIT_FUNCTION(pstat)
I'm writing a small webapp in Grails and I have the following question regarding
I'm currenty writing small application for image processing. However I have a big problem
I am writing a small module which will have several different aspects to it,
I am writing small app, using Play Framework 2.0 which uses Ebean as ORM.
I have an application where I am reading and writing small blocks of data
I'm writing a small web app that will receive and parse tab-delimited text files
I writing small application in pure C++. But now I encourage strange problem. I
when writing small functions I often have the case that some parameters are given

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.