Wednesday, June 28, 2023

Web Design: Make Duplicate Forms And Remove/Reset Them

 I don't know if anyone else would ever need or want something like this, but here we go. If you want to be able to create multiple forms on demand on the same page along with maintaining the ability to remove them or interact with them, then this is for you. First thing first, we need to make an HTML document.

<!DOCTYPE html>

<html>

<head>

<title>Some Stupid Form Thing</title>

Now let's get our Javascript on! I know I add some stuff that is not needed, but I'm weird.

<script type="application/javascript">//<[CDATA[

"use strict";

//I like to do a global storage object

var storage = {

    "formdata": "",

    "fid": 1

}

// function to store original form setup

function getformdata() {

    storage["formdata"] = document.getElementById("form0").innerHTML;

}

//Creating new forms

function addform() {

    var newform = document.createElement("form");

    newform.id = "form" + storage["fid"]++;

    newform.innerHTML = storage["formdata"];

    document.getElementById("formlist").append(newform);

}

// Removing forms, or reset the last form

function removeform(b) {

    b.parentElement.remove();

    if (document.getElementById("formlist").children.length < 1) {

        storage["fid"] = 0;

        addform();

    }

}

//]]></script>

</head>

Now that all the delicious code is out of the way, we need to create the body that this will interact with. It's actually fairly simple.

<body onload="getformdata()">

<div id="formlist">

<form id="form0" action="#">

Employee ID: <input type="text" value="" class="eid" /><button type="button">Do a thing</button><br />

<button type="button" onclick="removeform(this)">Remove</button>

/form>

</div>

<button onclick="addform()">Add Form</button>

</body>

</html>

Summary time. I am using a div as a container to create and remove forms in. Using the DOM we can isolate what we need inside of whatever container and use that to modify whatever with minimal navigation, no extra overhead of frameworks needed. I don't know why anyone would need this, other than the reason I made it that I'm not going to say. But there you go.

No comments:

Post a Comment

Tag Cloud

.NET (2) A+ (5) ad ds (1) addon (4) Android (4) anonymous functions (1) application (9) arduino (1) artificial intelligence (1) backup (1) bash (6) camera (2) certifications (3) comptia (5) css (2) customize (11) encryption (3) error (13) exploit (5) ftp (1) funny (4) gadget (4) games (3) GUI (5) hardware (16) haskell (6) help (14) HTML (3) imaging (2) irc (1) it (1) java (2) javascript (13) jobs (1) Linux (19) lua (1) Mac (4) malware (1) math (6) msp (1) network (13) perl (2) php (3) plugin (2) powershell (8) privacy (2) programming (24) python (10) radio (2) regex (3) repair (2) security (16) sound (2) speakers (2) ssh (1) story (5) Techs from the Crypt (5) telnet (1) tools (13) troubleshooting (11) tutorial (9) Ubuntu (4) Unix (2) virtualization (2) web design (6) Windows (16) world of warcraft (1) wow (1) wx (1)