Saving data in JSON format at a particular location

My JSON data contains various items that need to be placed in specific positions within a form. Is it possible to achieve this?

var objs = [{
  "Object1": {
    "ID": 1,
    "type": "input",
    "color": "red",
    "Text": "DARKDRAGON",
    "width": "100px",
    "height": "100px",
    "top": "50%",
    "left": "25%",
    "Font": {
      "fontName": "tahoma",
      "font": "150px"
    }
  },
  "Object2": {
    "ID": 2,
    "type": "textarea",
    "color": "cyan",
    "Text": "SPEEDYTIGER",
    "width": "100px",
    "height": "100px",
    "top": "50%",
    "left": "25%",
    "Font": {
      "fontName": "tahoma",
      "font": "150px"
    }
  },
  "Object3": {
    "ID": 3,
    "type": "input",
    "color": "blue",
    "Text": "AMyesteriousAdults",
    "width": "100px",
    "height": "100px",
    "top": "50%",
    "left": "25%",
    "Font": {
      "fontName": "tahoma",
      "font": "150px"
    }
  },
  "Object4": {
    "ID": 4,
    "type": "button",
    "color": "darkorange",
    "Text": "AMyesteriousDarkSpeed",
    "width": "100px",
    "height": "100px",
    "top": "50%",
    "left": "25%",
    "Font": {
      "fontName": "tahoma",
      "font": "150px"
    }
  }
}]

objs.forEach(function(objs) {
  for (var k in objs) {
    if (objs[k] instanceof Object) {
      console.log(objs[k].type);
      var elmn = document.createElement(objs[k].type);
      elmn.textContent = objs[k].Text;
      elmn.style.color = objs[k].color;
      elmn.DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC = objs[k].top;
      document.getElementById('ColorArea').appendChild(elmn);
    } else {
      console.log('Else');
    };
  }
});
<!DOCTYPE html>
<html>

<head>
</head>

<body>
  <div id="ColorArea">
  </div>
  <script src="Main.js"></script>
</body>

</html>

I would like the items to be arranged one on the left, one at the bottom, or all placed under each other to ensure a well-organized appearance.

Answer №1

To customize the style properties, ensure that your container has a relative position while the items inside have an absolute position.

If you test the code snippet, remember to click on the Full page link to view it properly instead of in a squished preview.

Object.assign(elmn.style, {
  position   : 'absolute',
  color      : formItem.color,
  width      : formItem.width,
  height     : formItem.height,
  top        : formItem.top,
  left       : formItem.left,
  fontFamily : formItem.Font.fontName,
  fontSize   : formItem.Font.font,
});

var objs = [{
  "Object1": {
    "ID": 1,
    "type": "input",
    "color": "red",
    "Text": "DARKDRAGON",
    "width": "100px",
    "height": "100px",
    "top": "25%",
    "left": "25%",
    "Font": {
      "fontName": "tahoma",
      "font": "150px"
    }
  },
  "Object2": {
    "ID": 2,
    "type": "textarea",
    "color": "cyan",
    "Text": "SPEEDYTIGER",
    "width": "100px",
    "height": "100px",
    "top": "50%",
    "left": "50%",
    "Font": {
      "fontName": "tahoma",
      "font": "150px"
    }
  },
  "Object3": {
    "ID": 3,
    "type": "input",
    "color": "blue",
    "Text": "AMyesteriousAdults",
    "width": "100px",
    "height": "100px",
    "top": "50%",
    "left": "25%",
    "Font": {
      "fontName": "tahoma",
      "font": "150px"
    }
  },
  "Object4": {
    "ID": 4,
    "type": "button",
    "color": "darkorange",
    "Text": "AMyesteriousDarkSpeed",
    "width": "100px",
    "height": "100px",
    "top": "25%",
    "left": "50%",
    "Font": {
      "fontName": "tahoma",
      "font": "150px"
    }
  }
}]

objs.forEach(function(objs) {
  for (var k in objs) {
    if (objs[k] instanceof Object) {
      const formItem = objs[k];
      var elmn = document.createElement(objs[k].type);
      elmn.textContent = formItem.Text;
      
      Object.assign(elmn.style, {
        position: 'absolute',
        color: formItem.color,
        width: formItem.width,
        height: formItem.height,
        top: formItem.top,
        left: formItem.left,
        fontFamily: formItem.Font.fontName,
        fontSize: formItem.Font.font,
      });
      
      document.getElementById('ColorArea').appendChild(elmn);
    } else {
      console.log('Else');
    };
  }
});
html, body {
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 0;
}

#ColorArea {
  position: relative;
  background: #EEE;
  width: 100%;
  height: 100%;
}
<div id="ColorArea"></div>

Answer №2

Uncertain if this meets your requirements

const elements = {
    "Element1": {
        "ID": 1,
        "type": "input",
        "color": "green",
        "Text": "FIREPHOENIX",
        "width": "120px",
        "height": "120px",
        "top": "60%",
        "left": "30%",
        "Font": {
            "fontName": "arial",
            "font": "180px"
        }
    },
    "Element2": {
        "ID": 2,
        "type": "textarea",
        "color": "magenta",
        "Text": "FLYINGDRAGON",
        "width": "120px",
        "height": "120px",
        "top": "60%",
         "left": "30%",
        "Font": {
            "fontName": "arial",
            "font": "180px"
        }
    },
   "Element3": {
        "ID": 3,
        "type": "input",
        "color": "purple",
        "Text": "MysteryMagicBeast",
        "width": "120px",
        "height": "120px",
        "top": "60%",
        "left": "30%",
        "Font": {
            "fontName": "arial",
             "font": "180px"
        }
    },
    "Element4": {
        "ID": 4,
        "type": "button",
        "color": "gold",
        "Text": "MysticalGoldenWings",
        "width": "120px",
        "height": "120px",
        "top": "60%",
        "left": "30%",
        "Font": {
            "fontName": "arial",
            "font": "180px"
        }
    }
};

var base_node = document.getElementById("ColorZone");
var line_break = document.createElement("br");

Object.values(elements).forEach(function(item) {

  var new_element = document.createElement(item.type);
  new_element.style.display = "block";
  new_element.style.color = item.color;
  new_element.style.width = item.width;
  new_element.style.height = item.height;
  new_element.style.top = item.top;
    new_element.style.left = item.left;
  new_element.value = item.Text;

  base_node.appendChild(new_element);
  base_node.appendChild(line_break);
  
})

Answer №3

Could you please give this a try: The elements will be arranged vertically, one below the other.

<html>
  <head>
    <style>
      #ColorArea {
        display: flex;
        flex-direction: column;
        align-items: flex-start;
      }
    </style>
  </head>

  <body>
    <div id="ColorArea">
    </div>
    <script src="Main.js"></script>
  </body>

</html>

var objs = [{
    "Object1": {
        "ID": 1,
        "type": "input",
        "color": "red",
        "Text": "DARKDRAGON",
        "width": "100px",
        "height": "100px",
        "top": "50%",
        "left": "25%",
        "Font": {
            "fontName": "tahoma",
            "font": "150px"
        }
    },
    "Object2": {
        "ID": 2,
        "type": "textarea",
        "color": "cyan",
        "Text": "SPEEDYTIGER",
        "width": "100px",
        "height": "100px",
        "top": "50%",
        "left": "25%",
        "Font": {
            "fontName": "tahoma",
            "font": "150px"
        }
    },
    "Object3": {
        "ID": 3,
        "type": "input",
        "color": "blue",
        "Text": "AMyesteriousAdults",
        "width": "100px",
        "height": "100px",
        "top": "50%",
        "left": "25%",
        "Font": {
            "fontName": "tahoma",
            "font": "150px"
        }
    },
    "Object4": {
        "ID": 4,
        "type": "button",
        "color": "darkorange",
        "Text": "AMyesteriousDarkSpeed",
        "width": "100px",
        "height": "100px",
        "top": "50%",
        "left": "25%",
        "Font": {
            "fontName": "tahoma",
            "font": "150px"
        }
    }
}]

objs.forEach(function (objs) {
    for (var k in objs) {
        if (objs[k] instanceof Object) {
            var elmn = document.createElement(objs[k].type);
            elmn.textContent = objs[k].Text;
            elmn.style.color = objs[k].color;
            elmn.style.margin = '10px';
            elmn.style['word-break'] = 'break-word';
            elmn.style.width = objs[k].width;
            elmn.style.height = objs[k].height;
            document.getElementById('ColorArea').appendChild(elmn);
        } else {
            console.log('Else');
        };
    }
});

  • If you need to set the value for an input element, you can include an additional condition like
if(objs[k].type == 'input') {
   elmn.value = objs[k].Text;
} else {
   elmn.textContent = objs[k].Text;
}

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

Adding map markers from a JSON feed to your React Google Map - a step-by-step guide!

I'm currently working on a React project that involves integrating Google Maps: const MarkerComponent = ({text}) => <div>{text}</div>; export default class NavMap extends Component { constructor(props) { super(props); this.s ...

Performing a consistent influx of data into a MySQL database using Node.js encounters an issue: "Unable to enqueue Handshake as a Handshake has

I am trying to insert values into a database in a continuous manner. Here is the code I have attempted: var mysql = require("mysql"); const random = require("random"); var con = mysql.createConnection({ host: "xxx", user: "xxx", password: "xxx", ...

Is there a way to customize the CSS ul menu specifically for the JavaScript autocomplete feature?

I have created a search page with autocomplete for the first textbox, but I noticed that the ul/li CSS classes used for styling the main menu are impacting the JavaScript list. How can I override the menu styling to display a regular list format? Being a ...

Executing a function when a user chooses to exit a webpage using the @HostListener('window:beforeunload') method

Utilizing @HostListener('window:beforeunload') allows me to detect when a user navigates away from the page, prompting a dialog window to open. I wish for an event to be triggered or a method to be executed if the user chooses to leave the page. ...

Is it sufficient to only capture 4xx errors?

Is it necessary to catch both 4xx and 5xx errors, or is catching just 4xx errors sufficient? In regular testing of my code, when would a 5xx error even occur? ...

Issue with dynamic dropdown selection causing element to not display

My code is designed to call the addpoc() method on button click, which generates a set of input boxes and a dropdown. When the dropdown option personal/workmail is selected, an input box should appear based on the selection. The issue arises after the init ...

Adding miscellaneous PHP scripts

When a user clicks on the sample button, my PHP code gets appended. It works fine, but I want to clean up my process. After some research, I learned that using jQuery AJAX is the way to go. The only problem is, I'm not sure how to implement AJAX. I&ap ...

Pass data from Wordpress plugin to JavaScript function

I recently developed a plugin for a WordPress website. Inside the plugin, I am trying to call a JavaScript function (passing a string as a parameter) from a file that I have just enqueued. However, nothing seems to be happening. Can anyone help me identify ...

Having trouble with TypeScript error in React with Material-UI when trying to set up tabs?

I have developed my own custom accordion component hook, but I am encountering the following error export default const Tabs: OverridableComponent<TabsTypeMap<{}, ExtendButtonBase<ButtonBaseTypeMap<{}, "button">>>> Check ...

Introducing Vee Validate 3.x and the ValidationFlags data type definition

I'm currently struggling to locate and utilize the ValidationFlags type within Vee-Validate 3. Despite my efforts, I am encountering difficulties in importing it. I am aware that this type is present in the source code located here. However, when I a ...

I developed a digital Magic 8 Ball program, but unfortunately, it's only providing me with one response

I'm currently working on a Discord Bot for my friends and myself. One of the scripts I've created is an 8Ball script, but it's only giving me one answer. Here's the code snippet for my variable: var rand = ['Yes', 'No&apo ...

What is the correct way to update the state in an array of objects using useState?

I'm struggling to figure out how to use the React useState hook properly. In my todolist, I have checkboxes and I want to update the 'done' property to 'true' for the item that has the same id as the checkbox being clicked. Even th ...

Design your very own personalized Show Layout component

Currently, I'm in the process of creating a unique layout component to enhance the design of my Show page. I've encountered some inconsistencies with functionality, and my solution involves utilizing the Material-UI <Grid> component. While ...

A JavaScript plugin that offers functionality similar to the repeating options in Google Calendar for forms

Does anyone know of a JavaScript plugin that visually replicates the repeat options in Google Calendar? ...

Addressing file routes within my personalized npm bundle

I am in the process of developing a custom npm package called packer and experimenting with a configuration similar to webpack, where users can install my CLI tool to build their packages. Here is an example of a package.json file for a test package using ...

The JavaScript array on its second usage had a length of '0', yet it still contained objects within it

About the Task: This task involves working with two arrays: arrNumber, which is a number array, and arrString, which is a string array. The goal is to create a list of objects using the values from arrNumber, where the object keys are numbers or characte ...

Exploring the source of the "Unexpected token ;" issue in an Express-JS application

Working on a project with Parse and Express-JS, I encountered an issue when trying to display an EJS page. The error message Unexpected token ; appeared while running the command line parse develop MyApp in Terminal. Below is the full stack trace of the er ...

Clicking on a class within a single tag can be selected using

Currently facing a seemingly trivial issue that I can't quite figure out. I have a jQuery function that selects the class of a tag when clicked, but the problem is that it also selects every other tag beneath the clicked tag in structural order. Howev ...

Node functions continue to run smoothly without affecting the loop

I am working on a webdriverjs application and I am trying to determine when jQuery has finished its processes on the page. I have implemented some methods, but it seems that even when the condition is supposed to trigger an else statement and stop the loop ...

Tips for filling data tables with a JSON string

When I use sAjaxSource = "filepath/file", the data table populates successfully. However, when I try to pass PHP json data and collect it, the datatable does not populate the data. Is there a way for me to directly utilize the JSON data without fetching ...