Incorporate content upon button click using Javascript

Looking to create a unique Survey layout that includes:


  1. Question Name
  2. Options

The question name is editable and can be changed with a click.

Initially, there is one option with a checkbox to select it. This option should also update when clicked. Additionally, there's a button to add a new option to a specific question, allowing for multiple options to be selected.

Once the first question is edited, users should be able to add more questions and repeat the process. I have some code that allows for text editing on click, but I'm unsure how to add additional options or questions. My code looks like this:

function help() {
  // code to add another option
}
body {
  background-color: #00ffaa;
  font-family: Verdana;
  text-align: center;
}

.questionName {
  margin-top: 15px;
  font-size: 20px;
}

.optionName {
  margin-top: 8px;
  font-size: 15px;
  font-style: italic;
  margin-left: 605px;
}

.box {
  margin-top: 12px;
}

.option {
  display: flex;
}
<html>
  <head>
    <title>Your Survey Form</title>
    <link rel="stylesheet" href="DemoSurveyStyle.css">
  </head>
  <body>
    <form>
      <h1 id="myText" contenteditable="true">Survey Name</h1>
      <div id="question">
        <div class="questionName" contenteditable="true">Question</div>
        <div class="option">
          <div class="optionName" contenteditable="true">Option</div>
          <input type="checkbox" class="box">
        </div>
        <button id="addOpButton" onclick="help()">Add Option</button>
      </div>
    </form>
    <script src="DemoCode.js"></script>
  </body>
</html>

If you have a solution on how to achieve this, I would greatly appreciate your guidance. It seems straightforward, but I lack experience in implementing it.

Answer №1

Feel free to use the appendChild method for your element

I have made updates to the HTML content as well

function update() {
  // code to insert a new option
  var q = document.getElementById('question');
  const node = document.createElement('div');
  node.classList.add("option");
  node.innerHTML = '<div class="optionName" contenteditable="true">Option</div><input type="checkbox" class="box">';
  q.appendChild(node);
}
update()
body {
  background-color: #00ffaa;
  font-family: Verdana;
  text-align: center;
}

.questionName {
  margin-top: 15px;
  font-size: 20px;
}

.optionName {
  margin-top: 8px;
  font-size: 15px;
  font-style: italic;
  margin-left: 605px;
}

.box {
  margin-top: 12px;
}

.option {
  display: flex;
}
<html>
  <head>
    <title>Your Survey Form</title>
    <link rel="stylesheet" href="DemoSurveyStyle.css">
  </head>
  <body>
    <form>
      <h1 id="myText" contenteditable="true">Survey Name</h1>
      <div id="question">
        <div class="questionName" contenteditable="true">Question</div> 
      </div>
      <button type="button" id="addOpButton" onclick="update()">Add Option</button>
    </form>
    <script src="DemoCode.js"></script>
  </body>
</html>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How can we determine in JavaScript when AngularJS has completed compiling its view?

I am having an issue where I need to use a JQ plugin on elements that are defined in an AngularJS View template. Typically, I would call the plugin like this: $(function() { $( "#selectable" ).selectable(); }); However, this approach is not workin ...

Importing a JavaScript file into another JavaScript file

var FS = require('fs'); var Path = require('path'); var Jsonfile = require('jsonfile'); var search = function () {}; search.prototype.projectContainerDirPath = null; /* * interface */ search.prototype.setPaths = function ...

What is the best way to assign a value to the param tag of an applet using JavaScript variables

This is my external javaScript file named myJs.js function search(){ var hint = document.getElementById("sChoice").value; var item = document.getElementById("sKey").value; document.getElementById("c").value=hint; document.getElementById ...

MongoDB subfield pagination technique

I am looking to retrieve specific data from a sub-field in a MongoDB collection and implement pagination for it. Thank you! { "_id": "616d274c655e0000ee005f32", "subscription": [ { "account_admin&q ...

Is there a way to incorporate a Laravel foreach loop within a JavaScript file?

I recently added a select-box using jQuery: <span onclick="createProduct()">Add New<i class="fa fa-plus"></i></span> <script> function createProduct() { var html = ''; html += ' <div clas ...

Retrieve text content from a specified element on a different page, not the one currently loaded, by

After implementing this dynamic content script, I realized that it is working well for the current page. However, since I cannot access the code-behind in the asp.net project, I am facing an issue when trying to fetch the text from "div.sampleheading" on t ...

The word "await" is a special reserved keyword used to call functions within an async

Currently utilizing async-await: Whenever the function run invokes findLateastVersion, even though run function is marked as async, an error message pops up stating await is a reserved word. The findLateastVersion method returns a promise and based on va ...

Discovering the block's height with the power of Jquery

I have written a piece of code that sets the height of a block of text and then applies that height to an image within the block. However, the size of the image ends up being smaller than the size of the block. https://i.stack.imgur.com/QUFWy.png <scr ...

Issue with allowing the second floating div to occupy the remaining horizontal space inside the container div

Describing the scenario, there is a container division with two floating divisions of different widths. You can view it live by following this link (look for "Hotel booking" section): I have added a border to the second division. My goal is to make this s ...

Error encountered when uploading mp3 file to S3 node due to size limitation

Currently, I am utilizing Node to transmit mp3 files to Amazon S3. However, after uploading the file, it shows a size of 9.0 Bytes and when attempting to play the audio from the public URL, it does not work as expected. Here is my current code snippet: rou ...

Adding content to an empty element will not produce the desired result

I'm trying to show each character of a string, which is stored in an array, one at a time. However, when I use threadsleep(a) with the code found here: http://jsfiddle.net/thefiddler99/re3qpuoo/, all the characters are displayed at once. There seems t ...

Utilize React Native to continuously fetch and display data from this API

[React-Native] Seeking assistance on looping through JSON API for conditional rendering. As a beginner, I would greatly appreciate your guidance. Thank you. Find my code here: https://drive.google.com/file/d/0B3x6OW2-ZXs6SGgxWmtFTFRHV00/view Check out th ...

What is the reason behind caching form data in an ajax call?

After the first successful submission, I am facing an issue where the original form data is being re-sent even when new values are submitted for a second attempt. How can I reset and reload the data to avoid this problem? I have tried several approaches i ...

Using ng-init to pass a JSON object

I'm attempting to pass a JSON Object to my application using ng-init and the stringify method, but I am encountering an error. Instead of working as expected, I am getting a Lexer error. Lexer Error: Unexpected next character at columns 8-8 [#] in ex ...

Retrieving webpage information in XML structure with CURL

My goal is to retrieve data in both XML and HTML formats using the curl command. curl --data "<xml>" --header "Content-Type: xml" http://example.com curl --data "<html>" --header "Content-Type: html" http://example.com In both cases, I r ...

What is the rationale behind sending only one form?

Is there a way to send two forms with just one click? The first form uploads an image to the server, while the second form sends information to a database. However, only the "myform" seems to be sent. Any suggestions? <form action="uploadpic.php" met ...

Retrieving Information from Ajax Response Following a Successful Insert Query in Codeigniter

I am trying to use ajax method to insert form data into a database and then redirect it to the next page. I have successfully passed the data in ajax and inserted it into the database table. However, I am facing an issue with getting the generated referenc ...

What is the process for deleting headers in a $http.get call and simultaneously including "application/json" and "text/plain" under the "Accept" header?

I'm relatively new to angularjs and I'm currently working on implementing some new features to an existing Angular JS application. The current API calls are quite intricate so I decided to make a direct call. Below is the code snippet for my new ...

Positioning Avatar component at the center of Card component

Hello there, I am currently trying to center my <Avatar> elements using Material UI with React within the <Card> components. The layout appears very crowded right now. What would be the most effective method to achieve this? I attempted setti ...

Creating a fixed-item navbar for sm-md sizes in Bootstrap 4: Step-by-step guide

I'm looking to create a navigation bar that maintains its proportions in small and medium column sizes. I am inspired by the navbar design on nm.org and want to achieve a similar fixed proportion no matter the size of the viewport. I have experimente ...