The JavaScript function does not function properly when it is called from another function

I tried my hand at creating a simple website for personal use, where I could input and edit text in an organized manner. A key feature I wanted was the ability to add new text fields dynamically by clicking a button, but it turned out to be more challenging than anticipated.

Just to clarify, I am not a professional in any way. I am a complete beginner and have likely made numerous mistakes along the way.

Any assistance would be greatly appreciated! Here is the code snippet:

function EDITTHIS() {
  document.getElementById('DESCRIPTION').contentEditable = 'true';
  document.getElementById('DESCRIPTION').style = 'background:yellow';
}

function ENDTHIS() {
  document.getElementById('DESCRIPTION').contentEditable = 'false';
  document.getElementById('DESCRIPTION').style = 'background:palegreen';
}

function AMPLIFYER() {

  function CREATION(element, element_id, element_text) {

    var element_a = document.createElement(element);
    element_a.setAttribute("id", element_id);
    var element_text = document.createTextNode(elemet_name);
    element_a.appendChild(element_text);
    document.body.appendChild(element_a);

  }


  CREATION("DIV", "DIVTHAT", "Write Here.");
  CREATION("BUTTON", "EDITTHAT", "EDIT");
  CREATION("BUTTON", "DONETHAT", "DONE");
}

function CREATION(element, element_id, element_text) {

  var element_a = document.createElement(element);
  element_a.setAttribute("id", element_id);
  var element_text = document.createTextNode(elemet_name);
  element_a.appendChild(element_text);
  document.body.appendChild(element_a);

}
h1 {
  background-color: green;
  color: white;
  font-size: 105px;
  margin-top: 0px;
  margin-bottom: 50px;
  text-shadow: 0px 0px 5px #6de1ff;
  vertical-align: middle;
  line-height: 300px;
}

h2 {
  background-color: none;
  color: Black;
  font-size: 45px;
  margin-top: 50px;
  margin-bottom: 0px;
  text-shadow: 0px 0px 5px #6de1ff;
}

button {
  background-color: palegreen;
  border: solid green;
  color: black;
  padding: 15px 32px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  font-size: 16px;
}

div {
  min-height: 30px;
  width: 1140px;
  padding: 50px 50px;
  font-size: 26px;
}

body {
  background-image: url("BG.png");
  background-attachment: fixed;
  background-repeat: no-repeat;
  background-position: right bottom;
}
<html>

<head>
  <title>Welcome to FELRYN</title>
</head>

<body>
  <center>
    <h1>FELRYN</h1>
  </center>
  <h2>Description of World:</h2>
  <div id="DESCRIPTION" contentEditable="false" style="background:palegreen">Write the description here.</div>
  <br />
  <button id="WANNAEDIT?" onclick="EDITTHIS()"> Edit</button>
  <button id="DONEEDIT?" onclick="ENDTHIS()"> Done</button>
  <br />
  <br />
  <br />
  <button id="MULTIPLY!" onclick="AMPLIFYER()">Create New Feild.</button>
  <br />
  <br />
  <br />
</body>

</html>

Answer №1

To implement the changes, make sure to replace element_name with element_text. By passing that variable to creation, you can use the name feature effectively. Additionally, remember to incorporate a counter to maintain unique IDs for new editable divs. This solution is fully functional.

var count = 1;

function AMPLIFYER() {
  CREATION("DIV", "DIVTHAT", "Write Here.");
  CREATION("BUTTON", "EDITTHAT", "EDIT");
  CREATION("BUTTON", "DONETHAT", "DONE");
  count++
  moveAddBtn()
}

function CREATION(element, element_id, element_text) {
  var element_a = document.createElement(element);
  element_a.setAttribute("id", element_id + count);
  if (element_text == "EDIT") {
    element_a.setAttribute('onclick', "EDITTHIS(" + count + ")")
  }
  if (element_text == "DONE") {
    element_a.setAttribute('onclick', "ENDTHIS(" + count + ")")
  }
  var element_text = document.createTextNode(element_text);
  element_a.appendChild(element_text);
  document.body.appendChild(element_a);  
}

function moveAddBtn() {
  var element = document.getElementById("MULTIPLY!");
  element.parentNode.removeChild(element)
  var linebreak = document.createElement("br");
  document.body.appendChild(linebreak);
  document.body.appendChild(element);
}

function EDITTHIS(x) {
  let editDiv = document.getElementById('DIVTHAT' + x)
  editDiv.contentEditable = 'true';
  editDiv.style = 'background:yellow';
}

function ENDTHIS(x) {
  let editDiv = document.getElementById('DIVTHAT' + x)
  editDiv.contentEditable = 'false';
  editDiv.style = 'background:palegreen';
}
h1 {
  background-color: green;
  color: white;
  font-size: 105px;
  margin-top: 0px;
  margin-bottom: 50px;
  text-shadow: 0px 0px 5px #6de1ff;
  vertical-align: middle;
  line-height: 300px;
}

h2 {
  background-color: none;
  color: Black;
  font-size: 45px;
  margin-top: 50px;
  margin-bottom: 0px;
  text-shadow: 0px 0px 5px #6de1ff;
}

button {
  background-color: palegreen;
  border: solid green;
  color: black;
  padding: 15px 32px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  font-size: 16px;
  margin-top: 20px;
}

div {
  min-height: 30px;
  width: 1140px;
  padding: 50px 50px;
  font-size: 26px;
}

body {
  background-image: url("BG.png");
  background-attachment: fixed;
  background-repeat: no-repeat;
  background-position: right bottom;
}
   
<center>
  <h1>FELRYN</h1>
</center>

<h2>Description of World:</h2>
<div id="DIVTHAT0" contentEditable="false" style="background:palegreen">Write the description here.</div>
<button id="WANNAEDIT?" class="pDiv" onclick="EDITTHIS(0); return false;"> Edit</button>
<button id="DONEEDIT?" onclick="ENDTHIS(0); return false;"> Done</button>

<br />
<br />
<br />

<button id="MULTIPLY!" onclick="AMPLIFYER()">Create New Feild.</button>

<br />
<br />
<br />

Answer №2

To enable the edit and done buttons, you must remove the CREATION function from within the AMPLIFYER function. Check out the code snippet below to see how it works. Let me know if this solution addresses your query.

<html>

<head>

<title>Welcome to FELRYN</title>

<style>

h1{
background-color: green;
color: white;
font-size: 105px;
margin-top:0px;
margin-bottom: 50px;
text-shadow: 0px 0px 5px #6de1ff;
vertical-align: middle;
line-height: 300px;
}

h2{
background-color: none;
color: Black;
font-size: 45px;
margin-top:50px;
margin-bottom: 0px;
text-shadow: 0px 0px 5px #6de1ff;
}

button {
background-color: palegreen;
border: solid green;
color: black;
padding: 15px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
}

div {
min-height: 30px;
width: 1140px;
padding: 50px 50px;
font-size: 26px;
}

body{
background-image: url("BG.png");
background-attachment: fixed;
background-repeat: no-repeat;
background-position: right bottom;
}

</style>

</head>

<body>

<script>

function EDITTHIS(){
document.getElementById('DESCRIPTION').contentEditable='true'; 
document.getElementById('DESCRIPTION').style='background:yellow';
}

function ENDTHIS(){
document.getElementById('DESCRIPTION').contentEditable='false'; 
document.getElementById('DESCRIPTION').style='background:palegreen';
}

function AMPLIFYER() {
   
CREATION("DIV", "DIVTHAT", "Write Here.");
CREATION("BUTTON", "EDITTHAT", "EDIT");
CREATION("BUTTON", "DONETHAT", "DONE");
}

function CREATION(element, element_id, element_text){

var element_a = document.createElement(element);
element_a.setAttribute("id", element_id);
var element_text = document.createTextNode(elemet_name);
element_a.appendChild(element_text);
document.body.appendChild(element_a);

}

</script>

<center><h1>FELRYN</h1></center>

<h2>Description of World:</h2>
<div id="DESCRIPTION" contentEditable="false" style = "background:palegreen">Write the description here.</div>
<br />

<button id="WANNAEDIT?" onclick="EDITTHIS()"> Edit</button>
<button id="DONEEDIT?" onclick="ENDTHIS()"> Done</button>

<br />
<br />
<br />

<button id="MULTIPLY!" onclick="AMPLIFYER()">Create New Field.</button>

<br />
<br />
<br />

</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

Is it possible for the NextJS Client component to only receive props after rendering props.children?

Encountering a puzzling issue that has me stumped... In my setup, I have a server side component that fetches data and then sends it over to the client component, which is all pretty standard. Here's where things get weird... When I log the data on ...

I'm having difficulty mocking a function within the Jest NodeJS module

I have a module in NodeJS where I utilize a function called existsObject to validate the existence of a file in Google Storage. While testing my application with Jest 29.1.2, I am attempting to create a Mock to prevent the actual execution of this functio ...

Customize the CSS style of a component in VueJS

After creating styles for my website, I encountered a challenge. Users input specific content using a WYSIWYG editor, and I want this content to have a distinct style without affecting the rest of the site. Despite trying various methods like , I couldn& ...

Unraveling JSON Data from a PHP Website with Jquery

I need to retrieve data from a database and store it in an array, similar to the example below var locations = [ ['Current', 18.53515053, 73.87944794, 2], ['VimanNagar', 18.5670762, 73.9084194, 1] ]; To begin with, I have created a ...

Tips on implementing jQuery autocomplete feature using PHP, MySQL, and Ajax then retrieving the data to be passed to

I've successfully implemented a JavaScript code for my autocomplete search feature. However, the data returned by the search is currently hardcoded in the code snippet below. I'm looking to retrieve the data from MySQL using PHP instead. Could a ...

Using AngularJS location.path for unique custom URLs

Control Code: $scope.$on('$locationChangeStart', function () { var path = $location.path(); var adminPath = '/admin/' ; if(path.match(adminPath)) { $scope.adminContainer= function() { return true; }; }); HTML <div clas ...

Trigger the onclick method by selecting an icon within the MUI DataGrid

Currently, I am utilizing the default MUI Dialog model in the "DialogModel.js" component. Additionally, I have integrated another MUI DataGrid component as shown below: // RulesTable.js import * as React from 'react'; import { DataGrid } from &a ...

Display an image with only a portion visible, no canvas, no frame, and no need

My dilemma involves an img with a surrounding box div. http://jsfiddle.net/6d4yC/7/ 1) I am seeking to display only a portion of the image (250x150) without generating a white overlay when it is in its large size. Placing a #box1 div around the image has ...

Detach an item from its parent once it has been added to an array

Currently, I am facing an issue with a form on my blog. The blog is represented as an object that contains multiple content objects within it. I seem to be experiencing some confusion because the reactivity of the content I add to the Array persists with t ...

JavaScript progress bar with extended duration, inspired by the success-oriented approach

Currently, I am in search of a unique solution or plugin that can replicate the same effect as Kickstarter's crowd fund long term progression bar. It seems like existing solutions only offer short term dynamic progress bars that indicate the percentag ...

Create a div element using JQuery mobile

I am experiencing an issue where a div on my webpage is supposed to refresh every 3 seconds, but the CSS is not being rendered correctly after each refresh. Below is the code I am using: HTML: <div id="statusbar"></div> JavaScript: $( docu ...

How can I transfer information from an HTML file (using the ejs template engine) to Node.js?

Currently, I am working on my own project using a combination of Node.Js (Express), MongoDB, JavaScript/jQuery, and HTML with EJS as the template engine. So far, I have some understanding of how to send data from the Node.js router to views and how to sen ...

What do I do when I get a "findByIdAndUpdate is not a function" error from my controller after requiring the model

I am currently developing a web application for my company that allows us to access and manage a database of customers and their information using MongoDB, Mongoose, and Express. Our company specializes in reselling used copiers/printers and offering maint ...

Is it possible for a PHP form to generate new values based on user input after being submitted?

After a user fills out and submits a form, their inputs are sent over using POST to a specified .php page. The question arises: can buttons or radio checks on the same page perform different operations on those inputs depending on which one is clicked? It ...

Which is more accessible: a disabled textbox or a div with role="textbox"?

Imagine I have a form in which one of the fields (name) is always disabled. When it comes to accessibility, would it be more effective to use an input <label> Name <input role="textbox" aria-readonly="true" value="Joe Shmoe" /> < ...

Present XML data on an HTML page to generate interactive form features

I have an application that features a checkbox tree. I would like to automatically pre-select those checkboxes if the user had previously checked them. To achieve this, I receive XML data from my backend Perl script which contains information on which che ...

Having difficulty defining the dropdown boundary in Bootstrap 5

Previously, I had this code working perfectly in Bootstrap 4. <div class="dropdown"> <span type="button" class="dropdown" data-toggle="dropdown" data-boundary="viewport"> <img src ...

Adding a Material UI Tooltip to the header name of a Material UI Datagrid - a step-by-step guide!

I've nearly completed my initial project, but the client is requesting that I include labels that appear when hovering over specific datagrid cells. Unfortunately, I haven't been able to find any solutions on Google for adding a Material UI Tool ...

Is there a way to retrieve a value from localStorage and use it to automatically set the selected option in a UL-based dropdown list upon page load

I have implemented a unique UL-based dropdown list that I discovered here (specifically the third model) as the foundation for a role-based selection box for my users. To enhance user experience, I am storing their role selection in localStorage so they do ...

Could the issue lie with PHP or the caching system?

I'm encountering a frustrating issue with my simple test that I just can't seem to resolve. It seems like there's some sort of glitch involving images and PHP, as I can only display one image at a time. The script looks correct to me, so I&a ...