I'm working on a upload formula, where the user can click on a "add" button, a "select file button" and a "title field" will show up then.
If the user clicks again on the "add button", a new button and field show up and so on and so forth.
My problem: I need those elements in a div, to be able to style and change them a bit.
Adding the elements do work but for a reason, adding a div doesn't work, and haven't figured out, what do to.
Here is my code:
<!DOCTYPE html>
<html>
<head>
<title>TEST1</title>
<meta charset="UTF-8"/>
</head>
<body>
<div id="media">
<form>
<div>
<div id="container"> </div>
<input type="button" value="add" id="add" />
</div>
</form>
</div>
</body>
JavaScript Code
<script>
window.addEventListener("load", function()
{
document.getElementById("add").addEventListener("click", function()
{
// Create a div
var container = document.createElement("div");
container.setAttribute("class", "new_container[]");
// Create a file input
var file = document.createElement("input");
file.setAttribute("type", "file");
file.setAttribute("name", "sel_ad_file[]");
// Create a break
var br = document.createElement("br");
// Create a text input
var text = document.createElement("input");
text.setAttribute("type", "text");
text.setAttribute("name", "ad_file_title[]");
text.setAttribute("placeholder", "File Title");
// add the file and text to the div
container.appendChild(file);
container.appendChild(br);
container.appendChild(text);
//Append the div to the container div
document.getElementById("media").appendChild(container);
});
});
</script>
CSS Code
<style>
* {margin: 0; padding: 0; font-family: ubuntu;}
body
{
overflow: hidden;
background-color: rgba(75,75,75);
z-index: -10;
}
#media
{
width: 350px;
height: 100%;
top: 0px;
left: 0px;
position: absolute;
background-color: rgba(30,30,30);
float: left;
}
#media input[type="button"]
{
width: 75px;
height: 25px;
top: 5px;
left: 355px;
position: absolute;
color: orange;
border-style: none;
background-color: rgba(40,40,40);
cursor: pointer;
}
#media input[type="button"]:hover
{
background-color: rgba(50,50,50)
}
.new_container
{
width: 250px;
height: 250px;
top: 0px;
left: 0px;
background-color: rgba(35,35,35);
position: absolute;
z-index: 1;
border-radius: 10px;
float: left;
}
.new_container input[type="file"]
{
width: 90%;
height: 25px;
transform: translate(-50%, -50%);
background-color: orange;
border-style: none;
position: absolute;
top: 0px;
left: 50%;
margin: 0px;
padding: 0px;
z-index: 2;
}
.new_container input[type="text"]
{
width: calc(90% - 10px);
height: 25px;
background-color: rgba(35,35,35);
border-style: none;
transform: translate(-50%, -50%);
border: solid 1px rgba(255,255,255,.5);
padding-left: 5px;
padding-right: 5px;
border-radius: 3px;
position: relative;
top: 150px;
left: 50%;
margin: 0px;
padding: 0px;
z-index: 2;
}
</style>
. . .
</html>
window.addEventListener("load", function()
{
document.getElementById("add").addEventListener("click", function()
{
// Create a div
var container = document.createElement("div");
container.setAttribute("class", "new_container[]");
// Create a file input
var file = document.createElement("input");
file.setAttribute("type", "file");
file.setAttribute("name", "sel_ad_file[]");
// Create a break
var br = document.createElement("br");
// Create a text input
var text = document.createElement("input");
text.setAttribute("type", "text");
text.setAttribute("name", "ad_file_title[]");
text.setAttribute("placeholder", "File Title");
// add the file and text to the div
container.appendChild(file);
container.appendChild(br);
container.appendChild(text);
//Append the div to the container div
document.getElementById("media").appendChild(container);
});
});
* {margin: 0; padding: 0; font-family: ubuntu;}
/*----------------------------------------------------------------------------*/
body
{
overflow: hidden;
background-color: rgba(75,75,75);
z-index: -10;
}
/*============================================================================*/
#media
{
width: 350px;
height: 100%;
top: 0px;
left: 0px;
position: absolute;
background-color: rgba(30,30,30);
float: left;
}
#media input[type="button"]
{
width: 75px;
height: 25px;
top: 5px;
left: 355px;
position: absolute;
color: orange;
border-style: none;
background-color: rgba(40,40,40);
cursor: pointer;
}
#media input[type="button"]:hover
{
background-color: rgba(50,50,50)
}
.new_container
{
width: 250px;
height: 250px;
top: 0px;
left: 0px;
background-color: rgba(35,35,35);
position: absolute;
z-index: 1;
border-radius: 10px;
float: left;
}
.new_container input[type="file"]
{
width: 90%;
height: 25px;
transform: translate(-50%, -50%);
background-color: orange;
border-style: none;
position: absolute;
top: 0px;
left: 50%;
margin: 0px;
padding: 0px;
z-index: 2;
}
.new_container input[type="text"]
{
width: calc(90% - 10px);
height: 25px;
background-color: rgba(35,35,35);
border-style: none;
transform: translate(-50%, -50%);
border: solid 1px rgba(255,255,255,.5);
padding-left: 5px;
padding-right: 5px;
border-radius: 3px;
position: relative;
top: 150px;
left: 50%;
margin: 0px;
padding: 0px;
z-index: 2;
}
<html>
<head>
<title>TEST1</title>
<meta charset="UTF-8"/>
</head>
<body>
<div id="media">
<form>
<div>
<div id="container"> </div>
<input type="button" value="add" id="add" />
</div>
</form>
</div>
</body>
</html>