Is there a way to convert the structure of HTML/CSS into JSON format?

    <!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
.collapsible {
background-color: #004c97;
color: white;
cursor: pointer;
padding: 18px;
width: 100%;
border: none;
text-align: left;
outline: none;
font-size: 15px;
font-family: Arial;
}

.active, .collapsible:hover {
background-color: #FFC61E;
}

.content {
padding: 0 18px;
display: none;
overflow: hidden;
background-color: #f1f1f1;
font-family: Arial;
}
</style>
</head>

<body>

<h1>User_Testing1</h1>
<button class="collapsible">This is the first tab</button>
<div class="content">
<p>This is the sub-category of the first tab.</p>
</div>
<button class="collapsible">This is the second tab</button>
<div class="content">
<p>This is the sub-category of the second tab.</p>
</div>
<button class="collapsible">This is the third tab</button>
<div class="content">
<p>This is the sub-category of the third tab.</p>
</div>

<button class="collapsible">This is the fourth tab</button>
<div class="content">
<p>This is the sub-category of the fourth tab,</p>
</div>

<script>
var coll = document.getElementsByClassName("collapsible");
var i;

for (i = 0; i < coll.length; i++) {
coll[i].addEventListener("click", function() {
this.classList.toggle("active");
var content = this.nextElementSibling;
if (content.style.display === "block") {
content.style.display = "none";
} else {
content.style.display = "block";
}
});
}
</script>

</body>
</html>

Hello! I am a new enthusiast in JSON/HTML programming. This HTML webpage was built using HTML/CSS, and now I aim to transform the code into JSON format. Despite watching numerous tutorial videos and reading articles on the subject, my search has mostly led me to table creations in HTML/JSON. Any assistance on converting this code would be greatly appreciated. Thank you for your help!

Answer №1

It appears that you are blending different ideas together. JSON serves as a way to convey data in a format that is easily understandable by both humans and machines. It functions simply as a format.

If you were to transform your HTML page into JSON, could you do it? The answer is yes, but there would be no advantage in doing so.

var json = {
  html: '<h1>User_Testing1</h1> <button class="collapsible">This is the first tab</button> <div class="content"> <p>This is the sub-category of the first tab.</p> </div> <button class="collapsible">This is the second tab</button> <div class="content"> <p>This is the sub-category of the second tab.</p> </div> <button class="collapsible">This is the third tab</button> <div class="content"> <p>This is the sub-category of the third tab.</p> </div> <button class="collapsible">This is the fourth tab</button> <div class="content"> <p>This is the sub-category of the fourth tab,</p> </div>',
  css: '.collapsible { background-color: #004c97; color: white; cursor: pointer; padding: 18px; width: 100%; border: none; text-align: left; outline: none; font-size: 15px; font-family: Arial; } .active, .collapsible:hover { background-color: #FFC61E; } .content { padding: 0 18px; display: none; overflow: hidden; background-color: #f1f1f1; font-family: Arial; }',
  javascript: 'var coll = document.getElementsByClassName("collapsible"); var i; for (i = 0; i < coll.length; i++) { coll[i].addEventListener("click", function() { this.classList.toggle("active"); var content = this.nextElementSibling; if (content.style.display === "block") { content.style.display = "none"; } else { content.style.display = "block"; } }); }'
};

var page = '<!DOCTYPE html><html><head><meta name="viewport" content="width=device-width, initial-scale=1">' + '<style>' + json.css + '</style>' + '</head><body>' + json.html + '<script>' + json.javascript + '<\/script>' + '</body></html>';

document.write(page);
document.close()

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

Utilizing Wordpress post images to dynamically change background URLs in CSS

Is there a way to dynamically set a background image in CSS using PHP, specifically the Wordpress post image? background: <?php if(has_post_thumbnail()){ $img = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'dest ...

If the ID (i.e. document.getElementById) is not found, then keep JavaScript running

I'm currently working on a JavaScript project where I need the script to gracefully handle missing div-ids and continue executing. I've looked into various solutions, but many involve either replacing the missing ID or placing information into an ...

Is it possible to create a mouse-following effect with lighting using Javascript

Recently, I've been honing my Javascript skills and decided to create a follow-mouse function. After successfully implementing it, I started brainstorming a new concept that I'm unsure is achievable. Is there a way to develop an "orb of vision" ...

The system encountered an issue with converting org.json.JSONObject to JSONArray

I am having an issue with a PHP file in my Android project. When I try to display the array data from my PHP code, I encounter an error stating "error parsing data org.json.JSONException ... error org.json.JSONObject cannot be converted to JSONArray." Bel ...

What is the best way to prompt users to submit comments with a popup textarea box?

Within my project, I have incorporated a dropdown list: <div class="dropdown"> <button class="btn btn-primary dropdown-toggle" type="button" data-toggle="dropdown">Select subject <span class="caret"></span> </but ...

Decoding a complex JSON data structure located within an array of items

Searching for data on Wind / Direction values has been challenging. An attempt I made using jq in a bash script: cat accuweather_raw | jq ".Wind[].Direction[] | [.Localized]" Unfortunately, I keep encountering the error "cannot index array with ...

Changing HTML dynamically does not trigger the ng-click event within ng-bind-html

I have developed a custom directive that can display messages along with rendering HTML content that may contain Angular attributes like buttons, anchor tags with ng-click attribute, and more. index.html: <ir-error-panel status="status"></ir-err ...

JavaScript influences CSS in EasySlider 1.7, creating a harmonious relationship between the

Struggling to achieve a captivating overlay effect with a slider using EasySlider 1.7, incorporating both images and corresponding text. However, encountering difficulties in aligning the text correctly on the page. The black overlay visible on the main s ...

Utilizing Conditional Styling for an Array of Objects within a Textarea in Angular 6

My array contains objects structured as follows: list =[ { name:"name1", value:true } { name:"name2", value:false } { name:"name3", value:true } { name:"name4", value:false ...

Mastering the correct method for passing $NODE_DEBUG_OPTION to npm-run-all in IntelliJ IDEA

Running on my system with Ubuntu 16.04, I have IntelliJ IDEA Ultimate 2017.2, node v6.11.2, and npm v3.10.10. I am trying to debug a node.js application that has the following package.json start entry: "start:" "npm-run-all --parallel serve-static open-st ...

Eliminate any additional spacing or padding surrounding the text input field

Is there a way to adjust the inner padding of a text field? Currently, when entering text in an HTML text field, there is padding at the beginning and end. The numbers I am inputting have no more than 3 digits, and the current padding takes up about the wi ...

Using Ruby on Rails to render a partial template inside a JSON object array

Struggling to include a partial in an ajax request with Ruby on Rails... Here is my array variable: @return = { :error => false, :response => "Added", :partial => ... } render :json => ActiveSupport::JSON.encode( @return ) Replace ... with ...

Calculating the aggregate value of MySQL based on the keys in a JSON group

Is there a way to aggregate values from a JSON table grouped by keys in MySQL version 5.7.12? MYSQL version: 5.7.12 table - +------+--------------------------------------+ | col1 | col2 | +------+------------------------ ...

Step-by-Step Guide: Customize the Background Color in an Angular 4 Select Dropdown Based on the Selected Option

I'm facing a challenge in setting the background color of a select element based on the color attribute of its currently selected option. I'm using Angular 4 for this task. An example of what I am attempting to accomplish is shown below: <se ...

Ways to retrieve information from a database using a specific ID stored in a JSON format

My device has generated a JSON file with an ID that needs to be deserialized. I want to use this ID as a parameter to select data from my database. Can anyone assist me with this? ...

Is the use of div:after content really affecting the width? I am having trouble getting my transition to work

Here is a simple code snippet that represents my actual code: #myDiv { background: black; color:white; float:left; min-width:45px; max-width:450px; -webkit-transition: all 1s ease-in-out; transition: all 1s ease-in-out; } #myDiv:hover:after { width ...

Trigger an alert when a button is clicked and redirect the user to an newly opened tab

I recently created a button with a link that opens in a new tab. I also implemented some JavaScript to display an alert. Everything is working as expected, but after the user clicks "OK" on the alert, they remain on the same page. I would like to automati ...

Can a radio button set be designed in two dimensions?

There are three sets of data (dataX, dataY, dataZ) that need to be assigned unique values out of a selection of three options (valueA, valueB, valueC). Each piece of data should be linked to one value and each value should have only one data associated wit ...

Tips for filtering out various values from a JSON String within a stored procedure in MS SQL

Looking for assistance with manipulating a JSON string. Here is the initial JSON: { "Country": { "Layer4": [ { "ItemName": "Cabinet MT", "ItemId": "cc3b0435-9ff5-4fd8-9f49-e ...

Tips for enabling resize functionality on individual components one at a time

Check out my Codepen link for the resizable event While using Jquery UI resizable, everything is functioning correctly. However, I am now looking to have the resizable event activate one block at a time. When another block is clicked, the resizable event ...