Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"liveServer.settings.port": 5501
}
48 changes: 48 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<!DOCTYPE html>
<html lang="en">
<head>
<link
rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css"
integrity="sha384-xOolHFLEh07PJGoPkLv1IbcEPTNtaed2xpHsD9ESMhqIYd0nLMwNLD69Npy4HI+N"
crossorigin="anonymous"
/>
<link rel="stylesheet" href="style.css" />
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>ReReddit</title>
</head>
<body>
<div class="container">
<div class="row justify-content-center">
<div class="col-md-6 mt-5">
<h1 class="text-center mb-4">ReReddit</h1>

<div class="posts mb-4"></div>

<form onsubmit="event.preventDefault()">
<div class="form-group">
<input
type="text"
id="post-text"
class="form-control"
placeholder="Post Text"
/>
</div>
<div class="form-group">
<input
type="text"
id="name"
class="form-control"
placeholder="Your Name"
/>
</div>
<button id="submit" class="btn btn-primary">Submit Post</button>
</form>
</div>
</div>
</div>

<script src="main.js"></script>
</body>
</html>
96 changes: 96 additions & 0 deletions main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
document.querySelector("form").addEventListener("submit", function (e) {
e.preventDefault();
});

//Event Listener on the Submit button
document.querySelector("#submit").addEventListener("click", function () {
var inputText = document.querySelector("#post-text").value;
var inputName = document.querySelector("#name").value;
var postsDiv = document.querySelector(".posts");

// Create div for posts
var div = document.createElement("div");
div.style.backgroundColor = "lightblue";
div.style.marginBottom = "10px";
div.style.padding = "10px";

// Create header wrapper
var headerDiv = document.createElement("div");
headerDiv.classList.add("post-header");

// Create button to remove posts
var removeButton = document.createElement("button");
removeButton.innerHTML = "remove";
removeButton.type = "button";
removeButton.className = "btn btn-primary";
removeButton.addEventListener("click", function () {
div.remove();
});

// Create button to add comments
var addComments = document.createElement("button");
addComments.innerText = "comments";
addComments.type = "button";
addComments.className = "btn btn-primary";

// Comments Section
var commentsSection = document.createElement("div");
commentsSection.classList.add("hidden");

var commentsTextInput = document.createElement("input");
commentsTextInput.type = "text";
commentsTextInput.style.margin = "10px";
commentsTextInput.placeholder = "Comment Text";
commentsTextInput.className = "form-control";

var commentsNameInput = document.createElement("input");
commentsNameInput.type = "text";
commentsNameInput.style.margin = "10px";
commentsNameInput.placeholder = "Your Name";
commentsNameInput.className = "form-control";

var submitCommentButton = document.createElement("button");
submitCommentButton.type = "button";
submitCommentButton.style.margin = "10px";
submitCommentButton.className = "btn btn-primary";
submitCommentButton.innerHTML = "Submit Comment";

commentsSection.append(commentsTextInput);
commentsSection.append(commentsNameInput);
commentsSection.append(submitCommentButton);

addComments.addEventListener("click", function () {
commentsSection.classList.toggle("hidden");
});

// Event listener on the submit comments button
submitCommentButton.addEventListener("click", function () {
var commentTextValue = commentsTextInput.value;
var commentNameValue = commentsNameInput.value;

var commentText = document.createElement("p");
commentText.innerText = `${commentTextValue} - Posted By: ${commentNameValue}`;

commentsSection.append(commentText);

commentsTextInput.value = "";
commentsNameInput.value = "";
});

var text = document.createElement("p");
text.innerText = `${inputText} - Posted By: ${inputName}`;

// Append comments and button to the header div
headerDiv.append(removeButton);
headerDiv.append(addComments);
headerDiv.append(text);

// Append the header div + the comments section plus the posts
div.append(headerDiv);
div.append(commentsSection);
postsDiv.append(div);

// Clear the input placeholders
document.querySelector("#name").value = "";
document.querySelector("#post-text").value = "";
});
20 changes: 20 additions & 0 deletions style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
.form-group {
padding-bottom: 0;
}
.hidden {
display: none;
}

.post-header {
display: flex;
align-items: center;
gap: 15px;
}

.posts button {
background: none;
border: none;
color: blue;
cursor: pointer;
padding: 0;
}