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 9134181
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:36:54+00:00 2026-06-17T08:36:54+00:00

I am attempting to create a div element and when it is clicked it

  • 0

I am attempting to create a div element and when it is clicked it creates a new element. In my case the new element is a styled p element. When the p element is clicked it creates a new li element within the p element. I made this work by having one large function but I am attempting to structure my code. My new structuring is clarifying but presenting me problems.

The below code works except when I click on the grey circles it does not create a new small green circle, which is what it should do. The error I get says the synth variable is not accessible. However, when I make the synth variable global I get another problem, I can only launch the createSynth function once…. and hence I can’t create multiple grey circles.

So in short I think the trick is to make the synth variable local and globally accessible (or at least accessible to the function that needs it…global variables are BAD!) From what I’ve read this is the perfect case for using a closure. However my understanding of closures is hazy and I figure this might be the optimal example for me to actually get my hands on one and "really get it" assuming I can be lead to water so-to-speak. Below is the code as well as a JS fiddle with the html and css element.

I think I could probably solve this by just passing a function to an argument (some how) but I am always curious how others more experienced than I am solve problems and structure code.

http://jsfiddle.net/taoist/rfFtY/1/

$(function() {

var orangeButton;
var applicationArea = document.getElementById("applicationArea"),
orangeButton = document.getElementById("orangeButton"),
counter = 1;




var createSynth = function() {
    var synth = document.createElement("p"); 
    synth.id = "synth" + (counter++);
    applicationArea.appendChild(synth);
    $(synth).draggable({ snap: true });
    $(synth).draggable({ grid: [ 20,20 ]});


};



var createParameter = function () {
    var synthParams = document.createElement("li");
    synthParams.id = "synthParams" + (counter++);
    synth.appendChild(synthParams);
    $(synthParams).draggable({ snap: true });
    $(synthParams).draggable({ grid: [ 20,20 ] });

};




orangeButton.addEventListener("click",createSynth, false);
synth.addEventListener("dblclick",createParameter, false);



});
  • 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-17T08:36:55+00:00Added an answer on June 17, 2026 at 8:36 am

    I would solve it this way:

    var createSynth = function() {
        var synth = document.createElement("p"); 
        synth.id = "synth" + (counter++);
        applicationArea.appendChild(synth);
        $(synth).draggable({ snap: true });
        $(synth).draggable({ grid: [ 20,20 ]}); 
        synth.addEventListener("dblclick",createParameter, false); 
    
    };
    

    I added event listener to synth at the end of this function, so every created synth can react to events.

    var createParameter = function () {
        var synthParams = document.createElement("li");
        synthParams.id = "synthParams" + (counter++);
        this.appendChild(synthParams);
        $(synthParams).draggable({ snap: true });
        $(synthParams).draggable({ grid: [ 20,20 ] });
    };
    
    orangeButton.addEventListener("click",createSynth, false);
    

    I used “this” instead of “synth”. As createParameter will handle an event for particular synth, that synth will become “this” in that function.

    JSFiddle: http://jsfiddle.net/sU3uy/1/

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

Sidebar

Related Questions

I'm new to javascript and I am attempting to create an element with certain
I am attempting to create a visual element using DIV elements and CSS which
I'm new to JQuery + JQuery mobile. I am attempting to create dynamic div
I attempting to create an 800x500 div with four overlapping divs inside the container
Im attempting to create a new operator :? on lists, which operates the same
I attempting to create custom tabs using this . But when I try to
I'm attempting to create a div tag and then alter it via css, but
I am attempting to create a simple texteditor with HTML5 contenteditable on a div
I'm attempting to create an animation by moving a sprite image across a div.
I am attempting to create a function where by a button is clicked and

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.