I have a simple note taking web application. Each time the user clicks on the '+' button a new note is added. Once he clicks on the '⟳' button all are excluded. I want to add five new item slots in the note that the user clicked. In order to do that I need to know which note he did so. This last bit is the one confusing me. How can I know which button the user clicked if all of the buttons are generated by the user?
HTML:
<html lang="en">
<head>
<title>To Do Lists</title>
<link rel="stylesheet" href="styles.css" />
<script src="script.js"></script>
</head>
<body>
<div class="top_bar">
<button id="plus">+</button>
<button id="restart">⟳</button>
</div>
<div id="notes" class="notes">
</div>
</body>
</html>
CSS
padding: 0px;
margin: 0px;
}
body{
display: flex;
flex-direction: column;
}
.top_bar{
width: 100%;
height: 10vh;
background-color: #95E29B;
display: flex;
align-items: center;
justify-content: space-between;
}
button{
font-size: 35px;
border: none;
width: 15%;
height: 10vh;
background-color: #3BCE4B;
cursor: pointer;
}
.notes{
width: 100%;
height: 90vh;
overflow: auto;
background-color: black;
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: center;
}
.note{
margin-top: 20px;
margin-bottom: 20px;
margin-left: 20px;
height: 40vh;
width: 30%;
border-radius: 10px;
background-color: white;
display: flex;
flex-direction: column;
justify-content: space-evenly;
align-items: flex-end;
}
.note_input{
margin-top: 20px;
margin-right: 5%;
font-size: 30px;
width: 90%;
border-style: solid;
border-top: none;
border-left: none;
border-right: none;
border-color: black;
}
form{
margin-top: 10px;
margin-right: 15%;
width: 80%;
height: 49%;
overflow-y: auto;
}
li{
border: none;
width: 70%;
display: flex;
}
.li_input{
border-style: solid;
border-color: black;
border-top: none;
border-left: none;
border-right: none;
margin-left: 10px;
font-size: 20px;
}
.more_items{
width: 35px;
height: 35px;
margin-right: 2%;
border-radius: 100%;
font-size: 20px;
}
JavaScript
const add_note = () => {
// Creates a new note and its props
const new_note = document.createElement("div");
const new_input = document.createElement("input");
const new_form = document.createElement("form");
const new_ol = document.createElement("ol");
const new_button = document.createElement("button");
//Populates the new note with inputs and checkboxes
for (let i = 0; i < 5; i++) {
let new_li = document.createElement("li");
let new_checkbox = document.createElement("input");
new_checkbox.setAttribute("type", "checkbox");
let new_li_input = document.createElement("input");
new_li_input.classList.add("li_input");
new_ol.appendChild(new_li);
new_li.appendChild(new_checkbox);
new_li.appendChild(new_li_input);
}
//New note's settings
new_note.classList.add("note");
new_note.appendChild(new_input);
new_input.classList.add("note_input");
new_input.setAttribute("placeholder", "Note's title");
new_note.appendChild(new_form);
new_ol.classList.add("ols");
new_form.appendChild(new_ol);
new_note.appendChild(new_button);
new_button.classList.add("more_items");
//Inserts the new note and button
const note_block = document.getElementById("notes");
note_block.appendChild(new_note);
new_button.addEventListener("click", add_more_items);
new_button.innerHTML = "+";
};
//Adds more items
const add_more_items = () => {
//console.log(new_button.parentElement.nodeName);
//let new_ol = document.getElementsByClassName("ols")[];
for (let i = 0; i < 5; i++) {
let new_li = document.createElement("li");
let new_checkbox = document.createElement("input");
new_checkbox.setAttribute("type", "checkbox");
let new_li_input = document.createElement("input");
new_li_input.classList.add("li_input");
new_ol.appendChild(new_li);
new_li.appendChild(new_checkbox);
new_li.appendChild(new_li_input);
}
};
//Removes all notes
const remove_note = () => {
let amount_of_notes = document.getElementsByClassName("note").length;
console.log(amount_of_notes);
while (amount_of_notes != 0) {
amount_of_notes--;
document.getElementsByClassName("note")[amount_of_notes].remove();
}
alert("All notes were removed.");
};
// Loads the buttons
const load_buttons = () => {
document.getElementById("plus").addEventListener("click", add_note);
document.getElementById("restart").addEventListener("click", remove_note);
};
// Main method
document.addEventListener("DOMContentLoaded", () => {
load_buttons();
});
Given your html is fairly simple, you can do this in a rudimentary style by making use of parentNode.
Your current code is erroring because you're trying to target new_ol to add the fresh <li> elements but it doesn't exist in scope of the add_more_items function. And even if it did, it would be ambiguous - which <ol> should it refer to?
Instead, you can work out the parent <ol> from the clicked button, like so:
const add_more_items = (e) => {
const new_ol = e.target.parentNode.querySelector('ol');
// rest of your code
}
Here's a full snippet putting all that together. I've put it in a codepen as the snippet editor here struggled with some parts of your layout: https://codepen.io/29b6/pen/qBxxXqG
Bear in mind that traversing the DOM like this isn't ideal. The main problem with this approach is that if your HTML structure changes, you can end up chaining multiple parentNodes together which gets ugly fast. But it should help you understand the concept of selecting an element's parent like you asked.
Related
Ive been studying javascript and following along on this online tutorial for a to-do list. I switched up and added a few of my own features, but I am not sure how would go about adding a feature where I can edit a individual list item?
I started off creating a function editTodo(key), I know I would have to append my new text to the old list text? If someone could give me a hint or guide me in the right direction?
//array that holds todo list items
let listItems = [];
//Function will create a new list item based on whatever the input value
//was entered in the text input
function addItem (text) {
const todo = {
text, //whatever user types in
checked: false, //boolean which lets us know if a task been marked complete
id: Date.now(), //unique identifier for item
};
//it is then pushed onto the listItems array
listItems.push(todo);
renderTodo(todo);
}
function checkDone(key) {
//findIndex is an array method that returns position of an element in array
const index = listItems.findIndex(item => item.id === Number(key));
//locates the todo item in the listItems array and set its checked property
//to opposite. 'true' will become 'false'
listItems[index].checked = !listItems[index].checked;
renderTodo(listItems[index]);
}
function deleteTodo(key) {
//find todo object in the listItems array
const index = listItems.findIndex(item => item.id === Number(key));
//create a new object with properties of the current list item
//delete property set to true
const todo = {
deleted: true,
...listItems[index]
};
//remove the list item from the array by filtering it out
listItems = listItems.filter(item => item.id !== Number(key));
renderTodo(todo);
}
//edits list item
function editTodo(key) {
//find todo object in the listItems array
const index = listItems.findIndex(item => item.id === Number(key));
}
//selects form element
const form = document.querySelector('.js-form');
const addGoal = document.getElementById('addBtn');
//adds a submit event listener
function selectForm(event) {
//prevent page refresh on form submission
event.preventDefault();
//select the text input
const input = document.querySelector('.js-todo-input');
//gets value of the input and removes whitespace beginning/end of string
//we then save that to new variable -> text
const text = input.value.trim();
//checks whether 2 operands are not equal, returning true or false (boolean)
//if input value is not equal to blank, add user input
if (text !== '') {
addItem(text);
input.value = ''; //value of text input is cleared by setting it to empty
input.focus(); //focused so user can add many items to list witout focusing the input
}
};
addGoal.addEventListener('click', selectForm, false);
form.addEventListener('submit', selectForm, false);
function renderTodo(todo) {
//saves local storage items, convert listItems array to JSON string
localStorage.setItem('listItemsRef', JSON.stringify(listItems));
//selects the first element with a class of 'js-to'list'
const list = document.querySelector('.js-todo-list');
//selects current todo (refer to top) list item in DOM
const item = document.querySelector(`[data-key='${todo.id}']`);
//refer to function deleteTodo(key)
if (todo.deleted) {
//remove item from DOM
item.remove();
return
}
//use the ternary operator to check if 'todo.checked' is true
//if true, assigns 'done' to checkMarked. if not, assigns empty string
const checkMarked = todo.checked ? 'done' : '';
//creates list 'li' item and assigns it to 'goal'
const goal = document.createElement('li');
//sets the class attribute
goal.setAttribute('class', `todo-item ${checkMarked}`);
//sets the data-key attribute to the id of the todo
goal.setAttribute('data-key', todo.id);
//sets the contents of the list item 'li'
goal.innerHTML = `
<input id="${todo.id}" type="checkbox" />
<label for="${todo.id}" class="tick js-tick"></label>
<span>${todo.text}</span>
<button class="edit-todo js-edit-todo"><i class="fa-solid fa-pencil"></i></button>
<button class="delete-todo js-delete-todo">X</button>
`;
//if item already exists in the DOM
if (item) {
//replace it
list.replaceChild(goal, item);
}else {
//otherwise if it doesnt (new list items) add at the end of the list
list.append(goal);
}
}
//selects entire list
const list = document.querySelector('.js-todo-list');
//adds click event listener to the list and its children
list.addEventListener('click', event => {
if (event.target.classList.contains('js-tick')) {
const itemKey = event.target.parentElement.dataset.key;
checkDone(itemKey);
}
//add this 'if block
if (event.target.classList.contains('js-delete-todo')) {
const itemKey = event.target.parentElement.dataset.key;
deleteTodo(itemKey);
}
})
//render any existing listItem when page is loaded
document.addEventListener('DOMContentLoaded', () => {
const ref = localStorage.getItem('listItemsRef');
if (ref) {
listItems = JSON.parse(ref);
listItems.forEach(t => {
renderTodo(t);
});
}
});
#import url('https://fonts.googleapis.com/css2?family=Montserrat&display=swap');
html {
box-sizing: border-box;
}
*, *::before, *::after {
box-sizing: inherit;
margin: 0;
padding: 0;
}
body {
font-family: 'Montserrat', sans-serif;
line-height: 1.4;
}
.container {
width: 100%;
max-width: 500px;
margin: 0 auto;
padding-left: 10px;
padding-right: 10px;
color: rgb(43, 43, 43);
height: 90vh;
margin-top: 20vh;
margin-bottom: 5vh;
overflow-y: auto;
}
.app-title {
text-align: center;
margin-bottom: 20px;
font-size: 80px;
opacity: 0.5;
}
.todo-list {
list-style: none;
margin-top: 20px;
}
.todo-item {
margin-bottom: 10px;
width: 100%;
display: flex;
align-items: center;
justify-content: space-between;
}
.todo-item span {
flex-grow: 1;
margin-left: 10px;
margin-right: 10px;
font-size: 22px;
}
.done span {
background-color:#0099e5;
color:#fff;
}
input[type="checkbox"] {
display: none;
}
#addBtn {
padding: 8px 16px;
font-size:16px;
font-weight:bold;
text-decoration: none;
background-color:#0099e5;
color:#fff;
border-radius: 3px;
border: 3px solid #333;
margin-left:10px;
cursor:pointer;
}
#addBtn:hover {
background-color:#0078b4;
}
.tick {
width: 30px;
height: 30px;
border: 3px solid #333;
border-radius: 50%;
display: inline-flex;
justify-content: center;
align-items: center;
cursor: pointer;
}
.tick::before {
content: '✓';
font-size: 20px;
display: none;
}
.done .tick::before {
display: inline;
}
.delete-todo {
border: none;
font-size:16px;
background-color:red;
color:#fff;
outline: none;
cursor: pointer;
width: 28px;
height: 28px;
border-radius:20px;
}
.edit-todo {
border: none;
font-size:16px;
background-color:green;
color:#fff;
outline: none;
cursor: pointer;
width: 28px;
height: 28px;
border-radius:20px;
}
.empty-warning {
flex-direction:column;
align-items:center;
justify-content:center;
display:none;
}
.todo-list:empty {
display:none;
}
.todo-list:empty + .empty-warning {
display:flex;
}
.empty-warning-title {
margin-top:15px;
opacity: 0.8;
color: rgb(43, 43, 43);
}
form {
width: 100%;
display: flex;
justify-content: space-between;
margin-left:5px;
}
input[type="text"] {
width: 100%;
padding: 10px;
border-radius: 4px;
border: 3px solid #333;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>To-Do List</title>
<link rel = "stylesheet" href = "style.css">
<script src="https://kit.fontawesome.com/67e5409c20.js" crossorigin="anonymous"></script>
</head>
<body>
<div class="container">
<h1 class="app-title">To Do List</h1>
<form class="js-form">
<input autofocus type="text" aria-label="Enter a new todo item" placeholder="Ex - Walk the dog" class="js-todo-input">
<input type="button" id="addBtn" value="Add">
</form>
<ul class="todo-list js-todo-list"></ul>
<div class="empty-warning">
<h2 class="empty-warning-title">Add your first goal</h2>
</div>
</div>
<script src="script.js"></script>
</body>
</html>
I added the edit feature to the event handler for click events on the list:
Figure I
if (event.target.matches('.edit-todo') && event.target !== event.currentTarget) {
const text = event.target.previousElementSibling;
text.toggleAttribute('contenteditable');
if (text.contenteditable) {
text.focus();
}
}
Basically, when the user clicks an edit button the contenteditable attribute is toggled (true/false) on the <span> that sits right before the button (hence .previousElementSibling).
I also added 2 CSS rulesets as well:
Figure II
.fa-pencil { pointer-events: none }
[contenteditable] { outline: 3px inset blue }
For some reason my mouse cannot click font-awesome icons, I have no idea why. So I disabled click events on the edit icon in order to click the edit button. Others might have the same problem as I do -- I'm 99% sure there's no harm in keeping that ruleset since it just makes the edit button 100% the origin element on the event chain. The second ruleset is a visual cue to the user that the <span> is editable.
let listItems = [];
function addItem(text) {
const todo = {
text,
checked: false,
id: Date.now(),
};
listItems.push(todo);
renderTodo(todo);
}
function checkDone(key) {
const index = listItems.findIndex(item => item.id === Number(key));
listItems[index].checked = !listItems[index].checked;
renderTodo(listItems[index]);
}
function deleteTodo(key) {
const index = listItems.findIndex(item => item.id === Number(key));
const todo = {
deleted: true,
...listItems[index]
};
listItems = listItems.filter(item => item.id !== Number(key));
renderTodo(todo);
}
function editTodo(key) {
const index = listItems.findIndex(item => item.id === Number(key));
}
const form = document.querySelector('.js-form');
const addGoal = document.getElementById('addBtn');
function selectForm(event) {
event.preventDefault();
const input = document.querySelector('.js-todo-input');
const text = input.value.trim();
if (text !== '') {
addItem(text);
input.value = '';
input.focus();
}
};
addGoal.addEventListener('click', selectForm, false);
form.addEventListener('submit', selectForm, false);
function renderTodo(todo) {
// localStorage.setItem('listItemsRef', JSON.stringify(listItems));
const list = document.querySelector('.js-todo-list');
const item = document.querySelector(`[data-key='${todo.id}']`);
if (todo.deleted) {
item.remove();
return
}
const checkMarked = todo.checked ? 'done' : '';
const goal = document.createElement('li');
goal.setAttribute('class', `todo-item ${checkMarked}`);
goal.setAttribute('data-key', todo.id);
goal.innerHTML = `
<input id="${todo.id}" type="checkbox" />
<label for="${todo.id}" class="tick js-tick"></label>
<span>${todo.text}</span>
<button class="edit-todo js-edit-todo"><i class="fa-solid fa-pencil"></i></button>
<button class="delete-todo js-delete-todo">X</button>
`;
if (item) {
list.replaceChild(goal, item);
} else {
list.append(goal);
}
}
const list = document.querySelector('.js-todo-list');
list.addEventListener('click', function(event) {
if (event.target.classList.contains('js-tick')) {
const itemKey = event.target.parentElement.dataset.key;
checkDone(itemKey);
}
if (event.target.classList.contains('js-delete-todo')) {
const itemKey = event.target.parentElement.dataset.key;
deleteTodo(itemKey);
}
if (event.target.matches('.edit-todo') && event.target !== event.currentTarget) {
const text = event.target.previousElementSibling;
text.toggleAttribute('contenteditable');
if (text.contenteditable) {
text.focus();
}
}
})
/*
document.addEventListener('DOMContentLoaded', () => {
const ref = localStorage.getItem('listItemsRef');
if (ref) {
listItems = JSON.parse(ref);
listItems.forEach(t => {
renderTodo(t);
});
}
});*/
#import url('https://fonts.googleapis.com/css2?family=Montserrat&display=swap');
html {
box-sizing: border-box;
}
*,
*::before,
*::after {
box-sizing: inherit;
margin: 0;
padding: 0;
}
body {
font-family: 'Montserrat', sans-serif;
line-height: 1.4;
}
.container {
width: 100%;
max-width: 500px;
margin: 0 auto;
padding-left: 10px;
padding-right: 10px;
color: rgb(43, 43, 43);
height: 90vh;
margin-top: 20vh;
margin-bottom: 5vh;
overflow-y: auto;
}
.app-title {
text-align: center;
margin-bottom: 20px;
font-size: 80px;
opacity: 0.5;
}
.todo-list {
list-style: none;
margin-top: 20px;
}
.todo-item {
margin-bottom: 10px;
width: 100%;
display: flex;
align-items: center;
justify-content: space-between;
}
.todo-item span {
flex-grow: 1;
margin-left: 10px;
margin-right: 10px;
font-size: 22px;
}
.done span {
background-color: #0099e5;
color: #fff;
}
input[type="checkbox"] {
display: none;
}
#addBtn {
padding: 8px 16px;
font-size: 16px;
font-weight: bold;
text-decoration: none;
background-color: #0099e5;
color: #fff;
border-radius: 3px;
border: 3px solid #333;
margin-left: 10px;
cursor: pointer;
}
#addBtn:hover {
background-color: #0078b4;
}
.tick {
width: 30px;
height: 30px;
border: 3px solid #333;
border-radius: 50%;
display: inline-flex;
justify-content: center;
align-items: center;
cursor: pointer;
}
.tick::before {
content: '✓';
font-size: 20px;
display: none;
}
.done .tick::before {
display: inline;
}
.delete-todo {
border: none;
font-size: 16px;
background-color: red;
color: #fff;
outline: none;
cursor: pointer;
width: 28px;
height: 28px;
border-radius: 20px;
}
.edit-todo {
border: none;
font-size: 16px;
background-color: green;
color: #fff;
outline: none;
cursor: pointer;
width: 28px;
height: 28px;
border-radius: 20px;
}
.empty-warning {
flex-direction: column;
align-items: center;
justify-content: center;
display: none;
}
.todo-list:empty {
display: none;
}
.todo-list:empty+.empty-warning {
display: flex;
}
.empty-warning-title {
margin-top: 15px;
opacity: 0.8;
color: rgb(43, 43, 43);
}
form {
width: 100%;
display: flex;
justify-content: space-between;
margin-left: 5px;
}
input[type="text"] {
width: 100%;
padding: 10px;
border-radius: 4px;
border: 3px solid #333;
}
.fa-pencil {
pointer-events: none
}
[contenteditable] {
outline: 3px inset blue
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>To-Do List</title>
<link rel="stylesheet" href="style.css">
<script src="https://kit.fontawesome.com/67e5409c20.js" crossorigin="anonymous"></script>
</head>
<body>
<div class="container">
<h1 class="app-title">To Do List</h1>
<form class="js-form">
<input autofocus type="text" aria-label="Enter a new todo item" placeholder="Ex - Walk the dog" class="js-todo-input">
<input type="button" id="addBtn" value="Add">
</form>
<ul class="todo-list js-todo-list"></ul>
<div class="empty-warning">
<h2 class="empty-warning-title">Add your first goal</h2>
</div>
</div>
<script src="script.js"></script>
</body>
</html>
My goal is to try to animate text. Every example on how to animate text looks at a singular string. I'd like to animate an array of string values, one after the other.
The problem I'm getting is that although I'm creating the elements needed successfully, only the .word3 class is actually being rendered. I'm not sure why this is and after banging my head against the wall, I'm not sure how to fix it.
I've used a lot of stack overflow resources to overcome things like using setTimeout in a for loop which lead to using an iife.. Here is the code that I've settled on for the time being. Using async caused a lot of issues as async needs to be at the top level apparently and I often got the 'unexpected reserved keyword error'.
There must be a simple way to do this?
All help is appreciated, thanks!
const wrapper = document.querySelector(".wrapper");
const buttonCreate = document.querySelector(".create-element");
const buttonAnimate = document.querySelector(".animation");
buttonCreate.addEventListener("click", createElement);
let i = 0;
let j = 0;
let sampleArray = ["Just", "another", "cool", "heading"];
function createElement() {
// debugger
// Create text wrapper in main body and add class
const newDiv = document.createElement("div");
newDiv.classList.add("text-wrapper");
wrapper.insertAdjacentElement("afterbegin", newDiv);
// Modifying text-wrapper html to include p tag with dynamic class
function word() {
sampleArray.map((word, i) => {
newDiv.innerHTML += `<p class="word${i}"></p>`;
let element = document.querySelector(`.word${i}`);
console.log(element);
let j = 0
let interval = setInterval(() => {
element.innerText += word[j];
j++;
if(j === word.length) {
clearInterval(interval)
}
}, 200)
});
};
word();
return;
}
html {
box-sizing: border-box;
}
*,*::before, *::after {
box-sizing: inherit;
}
body {
margin: 0;
}
.wrapper {
width: 100%;
background-color: #333;
color: #FFA;
text-align: center;
height: 100vh;
display: flex;
flex-direction: row;
gap: 10rem;
align-items: center;
justify-content: center;
font-size: 4rem;
position: relative;
}
.text-wrapper {
position: absolute;
top: 0;
width: 100%;
display: flex;
gap: 3rem;
flex-direction: column;
justify-content: center;
justify-content: space-around;
}
.button {
font-size: 3rem;
border-radius: 6px;
background-color: #47cefa;
}
.button:hover {
cursor: pointer;
background-color: #BCEF4D;
}
<section class="wrapper">
<button class="button create-element">Create Element</button>
<button class="button animation">Start Animation</button>
</section>
Instead of setInterval in a loop of the array, just create a recursive function that calls setTimeout and calls itself and increments the array counter until the end of the array.
The lay out of my answer is off because I'm not exactly sure on what your expected layout is
const wrapper = document.querySelector(".text-wrapper");
const buttonAnimate = document.querySelector(".animation");
buttonAnimate.addEventListener("click", animation);
let i = 0;
let sampleArray = ["Just", "another", "cool", "heading"];
function animation() {
if (i < sampleArray.length) {
let el = document.createElement("p");
el.innerHTML = sampleArray[i];
wrapper.appendChild(el);
i++;
setTimeout(animation, 200);
}
}
.wrapper {
width: 100%;
background-color: #333;
color: #FFA;
text-align: center;
height: 100vh;
display: flex;
flex-direction: row;
gap: 10rem;
align-items: center;
justify-content: center;
font-size: 4rem;
position: relative;
}
.text-wrapper {
position: absolute;
top: 0;
width: 100%;
display: flex;
gap: 3rem;
flex-direction: column;
justify-content: center;
justify-content: space-around;
}
.button {
font-size: 3rem;
border-radius: 6px;
background-color: #47cefa;
}
.button:hover {
cursor: pointer;
background-color: #BCEF4D;
}
<section class="wrapper">
<button class="button animation">Start Animation</button>
<div class="text-wrapper"></div>
</section>
I've been stuck with this problem for a while now and almost got it to work, but I'm left with an issue I cannot solve or find a solution anywhere.
So I'm trying to make an application which helps users log certain data - it all works on a table, adding rows and deleting them works just fine, but I have a problem with editing them.
At the end of each row, there are 2 buttons - delete and edit.
Edit pops up a modal, which should (and it does as far as I know) display inputs with values read from a specific table row. When you do this for the first time after reloading it works fine, but afterward, if you have more than one table rows it just starts to clean up these tr's upon submitting an edit of a row.
There's clearly a problem with logic, and I just cannot solve it. Most likely it can be found in this block
Record.prototype.addRow = function(){
let newLog = document.createElement('tr');
newLog.setAttribute('id',idContainer);
idContainer++;
recordTable.appendChild(newLog);
//store values and add them to each td
let valueStorage = [this.time,this.latitude,this.longitude,this.heading,
this.speed,this.wind,this.sea,this.visibility,this.remarks];
for (let i=0; i<9;i++) {
let tableData = document.createElement('td');
newLog.appendChild(tableData);
tableData.textContent = valueStorage[i];
}
//add 2 buttons - delete and edit
let deleteBtn = document.createElement('button');
deleteBtn.setAttribute('class','new-record__delete-row-btn')
newLog.appendChild(deleteBtn);
let editBtn = document.createElement('button');
editBtn.setAttribute('class','new-record__edit-row-btn')
newLog.appendChild(editBtn);
//adding functionality to edit/delete btns
function editThisRow(e){
//on pop up display values from the edited row
let popUpHeading = document.querySelector('.new-record__popup h2');
let thisRow = e.target.parentNode;
let editStorage = [];
for(let i = 0; i < 9; i++){
editStorage.push(thisRow.childNodes[i].textContent);
}
editStorage[1]=editStorage[1].replace(/([A-Z])/,"");
editStorage[2]=editStorage[2].replace(/([A-Z])/,"");
for(let i = 0; i <8; i++){
newRecordInputs[i].value = editStorage[i];
}
remarks.value = editStorage[8];
popUpHeading.textContent = 'Edit your record!'
openNewRecordPopup();
//adding event listener to the record edit button, so that it applies changes
const recordEditBtn = document.querySelector('#edit-record')
newRecordSubmitBtn.style.display = "none";
recordEditBtn.style.display = "block";
recordEditBtn.addEventListener('click',()=>{
let thisRowTds = thisRow.childNodes;
for(let i = 0; i < 8; i++){
thisRowTds[i].textContent = newRecordInputs[i].value
}
thisRowTds[8].textContent = remarks.value;
closeNewRecordPopup();
popUpHeading.textContent = 'Fill out inputs below to make a new record';
newRecordSubmitBtn.style.display = "block";
recordEditBtn.style.display = "none";
sortRecords();
})
}
deleteBtn.addEventListener('click',()=>{
newLog.remove();
})
editBtn.addEventListener('click',editThisRow);
}
All the code needed and live example can be found on GitHub:
https://github.com/michynow/electronic-ship-log-book
https://michynow.github.io/electronic-ship-log-book/
quick note: This is not designed for mobile use, and I really would appreciate vanilla JS solutions, without lib's or frameworks.
All edit clicks keep on stacking in below button:
const recordEditBtn = document.querySelector('#edit-record')
I have changed it to have fresh event handler always like this:
let oldRecordEditBtn = document.querySelector('#edit-record')
let recordEditBtn = oldRecordEditBtn.cloneNode(true);
oldRecordEditBtn.parentNode.replaceChild(recordEditBtn, oldRecordEditBtn);
So now below snippet is working fine for your issue:
//Set ship header and type
const shipDetailsBtn = document.querySelector('.ship-details__button');
const shipDetailsClosingBtn = document.querySelector('#ship-details__popup-closing-btn');
const shipDetailsPopUp = document.querySelector('.ship-details__popup');
const shipTypeSpan = document.querySelector('#ship-type-span');
const shipNameSpan = document.querySelector('#ship-name-span');
//opening of a form
shipDetailsBtn.addEventListener('click', () => {
shipDetailsPopUp.style.visibility = "visible";
disableButtons();
})
//disabling of button operation when popup is open;
function disableButtons() {
voyageDetailsBtn.disabled = true;
newRecordBtn.disabled = true;
shipDetailsBtn.disabled = true;
}
function enableButtons() {
voyageDetailsBtn.disabled = false;
newRecordBtn.disabled = false;
shipDetailsBtn.disabled = false;
}
//closing of a form
shipDetailsClosingBtn.addEventListener('click', () => {
shipDetailsPopUp.style.visibility = "hidden";
enableButtons();
})
//pop up input selection
const shipTypeSelect = document.querySelector('#ship-type');
const shipDetailsPopUpSubmit = document.querySelector('#ship-details-submit-btn');
//Submitting of ship details
shipDetailsPopUpSubmit.addEventListener('click', () => {
shipDetailsPopUp.style.visibility = "hidden";
shipTypeSpan.textContent = shipTypeSelect.options[shipTypeSelect.selectedIndex].value + " ";
shipNameSpan.textContent = document.querySelector('#ship-name-input').value;
enableButtons();
shipDetailsBtn.textContent = "Edit ship details";
})
//date and destination pop up form
const voyageDetailsBtn = document.querySelector('.voyage-details__button');
const voyageDetailsPopUp = document.querySelector('.voyage-details__popup');
const voyageDetailsClosingBtn = document.querySelector('#voyage-details__popup-closing-btn');
voyageDetailsBtn.addEventListener('click', () => {
voyageDetailsPopUp.style.visibility = "visible";
disableButtons();
})
//Update date and destination rows
const dateSpan = document.querySelector('#date-span');
const destinationSpanFrom = document.querySelector('#dest-span__from');
const destinationSpanTo = document.querySelector('#dest-span__to');
const voyageDetailsSubmit = document.querySelector('#voyage-details__submit-btn');
const destFromInput = document.querySelector('#ship-destination-input__from');
const destToInput = document.querySelector('#ship-destination-input__to');
voyageDetailsSubmit.addEventListener('click', () => {
dateSpan.textContent = " " + document.querySelector('#date-input').value;
//prevent empty inputs on destination form
if (destFromInput.value !== "" || destToInput.value !== "") {
destinationSpanFrom.textContent = " " + destFromInput.value + ' to: ';
destinationSpanTo.textContent = destToInput.value;
voyageDetailsBtn.textContent = "Edit date and destination";
voyageDetailsPopUp.style.visibility = "hidden";
enableButtons();
} else {
alert('Please fill in voyage details!');
}
})
//remember to add a default attribute setting the current date as placeholder in date form;
//closing of a voyage details pop up
voyageDetailsClosingBtn.addEventListener('click', () => {
voyageDetailsPopUp.style.visibility = "hidden";
enableButtons();
})
//add new data pop up opening / closing
const newRecordBtn = document.querySelector('.new-record__btn');
const newRecordPopUp = document.querySelector('.new-record__popup');
const newRecordSubmitBtn = document.querySelector('#new-record__submit-btn');
const recordTable = document.querySelector('.records-table');
newRecordBtn.addEventListener('click', openNewRecordPopup);
function openNewRecordPopup() {
newRecordPopUp.style.visibility = "visible";
disableButtons();
}
const newRecordClosingBtn = document.querySelector('#new-record__popup-closing-btn');
function closeNewRecordPopup() {
newRecordPopUp.style.visibility = "hidden";
enableButtons();
}
newRecordClosingBtn.addEventListener('click', closeNewRecordPopup);
// store input values in an array, then pass it to all created td's
let newRecordInputs = document.querySelectorAll('.new-record__popup input');
let remarks = document.querySelector('textarea');
function clearInputs() {
for (let i = 0; i < newRecordInputs.length; i++) {
newRecordInputs[i].value = "";
remarks.value = "";
};
}
newRecordSubmitBtn.addEventListener('click', addRecord);
class Record {
constructor(time, latitude, longitude, heading, speed, wind, sea, visibility, remarks) {
this.time = time;
this.latitude = latitude;
this.longitude = longitude;
this.heading = heading;
this.speed = speed;
this.wind = wind;
this.sea = sea;
this.visibility = visibility
this.remarks = remarks;
this.addRow();
}
}
//set id for each row
let idContainer = 0;
Record.prototype.addRow = function() {
let newLog = document.createElement('tr');
newLog.setAttribute('id', idContainer);
idContainer++;
recordTable.appendChild(newLog);
//store values and add them to each td
let valueStorage = [this.time, this.latitude, this.longitude, this.heading,
this.speed, this.wind, this.sea, this.visibility, this.remarks
];
for (let i = 0; i < 9; i++) {
let tableData = document.createElement('td');
newLog.appendChild(tableData);
tableData.textContent = valueStorage[i];
}
//add 2 buttons - delete and edit
let deleteBtn = document.createElement('button');
deleteBtn.setAttribute('class', 'new-record__delete-row-btn')
newLog.appendChild(deleteBtn);
let editBtn = document.createElement('button');
editBtn.setAttribute('class', 'new-record__edit-row-btn')
newLog.appendChild(editBtn);
//adding functionality to edit/delete btns
let editThisRow = function(e) {
//on pop up display values from the edited row
let popUpHeading = document.querySelector('.new-record__popup h2');
let thisRow = e.target.parentNode;
let editStorage = [];
for (let i = 0; i < 9; i++) {
editStorage.push(thisRow.childNodes[i].textContent);
}
editStorage[1] = editStorage[1].replace(/([A-Z])/, "");
editStorage[2] = editStorage[2].replace(/([A-Z])/, "");
for (let i = 0; i < 8; i++) {
newRecordInputs[i].value = editStorage[i];
}
remarks.value = editStorage[8];
popUpHeading.textContent = 'Edit your record!'
openNewRecordPopup();
//adding event listener to the record edit button, so that it applies changes
let oldRecordEditBtn = document.querySelector('#edit-record')
let recordEditBtn = oldRecordEditBtn.cloneNode(true);
oldRecordEditBtn.parentNode.replaceChild(recordEditBtn, oldRecordEditBtn);
newRecordSubmitBtn.style.display = "none";
recordEditBtn.style.display = "block";
recordEditBtn.addEventListener('click', () => {
let thisRowTds = thisRow.childNodes;
for (let i = 0; i < 8; i++) {
thisRowTds[i].textContent = newRecordInputs[i].value
}
thisRowTds[8].textContent = remarks.value;
closeNewRecordPopup();
popUpHeading.textContent = 'Fill out inputs below to make a new record';
newRecordSubmitBtn.style.display = "block";
recordEditBtn.style.display = "none";
sortRecords();
})
}
deleteBtn.addEventListener('click', () => {
newLog.remove();
})
editBtn.addEventListener('click', editThisRow);
}
function addRecord() {
//selecting all input values and storing them in an object
let timeValue = document.querySelector('#new-record__UTC-time').value;
let northOrSouth = document.querySelector('.north-south');
let northOrSouthValue = northOrSouth.options[northOrSouth.selectedIndex].value;
let latitudeValue = document.querySelector('#new-record__latitude').value + " " + northOrSouthValue;
let eastOrWest = document.querySelector('.east-west');
let eastOrWestValue = eastOrWest.options[eastOrWest.selectedIndex].value;
let longitudeValue = document.querySelector('#new-record__longitude').value + " " + eastOrWestValue;
let headingValue = document.querySelector('#new-record__heading').value;
let speedValue = document.querySelector('#new-record__SOG').value;
let windValue = document.querySelector('#new-record__wind-force').value;
let seaValue = document.querySelector('#new-record__sea-state').value;
let visibilityValue = document.querySelector('#new-record__visibility').value;
let anotherRecord = new Record(timeValue, latitudeValue, longitudeValue,
headingValue, speedValue, windValue, seaValue, visibilityValue, remarks.value);
closeNewRecordPopup();
clearInputs();
sortRecords();
}
//function for sorting out table rows by time dynamically upon edit or new record
function sortRecords() {
let rows, switching, i, x, y, shouldSwitch;
switching = true
while (switching) {
switching = false;
rows = recordTable.rows;
for (i = 2; i < (rows.length - 1); i++) {
shouldSwitch = false;
x = rows[i].getElementsByTagName("td")[0];
y = rows[i + 1].getElementsByTagName("td")[0];
if (x.textContent > y.textContent) {
shouldSwitch = true;
break;
}
}
if (shouldSwitch) {
rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
switching = true;
}
}
}
body {
z-index: 0;
padding-top: 2vh;
/*height: 100vh;
width: 100vw;*/
/*overflow: hidden;*/
margin: 0;
-webkit-box-sizing: border-box;
box-sizing: border-box;
font-family: 'Montserrat', sans-serif;
font-size: 14px;
background: -webkit-gradient(linear, left top, left bottom, color-stop(50%, white), to(#347deb));
background: linear-gradient(to bottom, white 50%, #347deb);
position: relative;
}
header {
text-align: center;
color: black;
}
header p,
header .destination {
text-align: left;
padding-left: 5vw;
font-weight: bold;
}
.records-table-container {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-orient: vertical;
-webkit-box-direction: normal;
-ms-flex-direction: column;
flex-direction: column;
width: 90vw;
height: 60vh;
margin: auto;
overflow-x: auto;
background-color: #faf6c3;
-webkit-box-shadow: 0px -1px 22px -3px rgba(0, 0, 0, 0.75);
box-shadow: 0px -1px 22px -3px rgba(0, 0, 0, 0.75);
}
.records-table-container .new-record__delete-row-btn,
.records-table-container .new-record__edit-row-btn {
padding: 0.3rem;
width: 60px;
margin: 0.1rem;
color: white;
font-weight: bold;
border: none;
cursor: pointer;
background-color: #963c2c;
-webkit-transition: 0.3s all;
transition: 0.3s all;
}
.records-table-container .new-record__delete-row-btn:hover,
.records-table-container .new-record__edit-row-btn:hover {
background-color: #803325;
}
.records-table-container .new-record__delete-row-btn::after,
.records-table-container .new-record__edit-row-btn::after {
content: "Delete";
}
.records-table-container .new-record__edit-row-btn {
background-color: #5a51d6;
}
.records-table-container .new-record__edit-row-btn::after {
content: "Edit";
}
.records-table-container .new-record__edit-row-btn:hover {
background-color: #4f47bf;
}
.records-table-container table,
.records-table-container th,
.records-table-container td,
.records-table-container tr {
border: 1px solid black;
padding: 5px 2px;
border-collapse: collapse;
text-align: center;
}
.records-table-container .table__input-general-description {
width: 40vw;
}
.operation-buttons {
text-align: center;
padding: 5vh;
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-orient: horizontal;
-webkit-box-direction: normal;
-ms-flex-direction: row;
flex-direction: row;
-ms-flex-pack: distribute;
justify-content: space-around;
}
.operation-buttons .new-record__btn {
border: none;
background-color: #4fa867;
color: white;
font-weight: bold;
padding: 10px;
-webkit-transition: background 0.3s;
transition: background 0.3s;
}
.operation-buttons .new-record__btn:hover {
background-color: #3f8a53;
cursor: pointer;
}
.operation-buttons .export-button {
border: none;
background-color: #963c2c;
color: white;
font-weight: bold;
padding: 10px;
-webkit-transition: background 0.3s;
transition: background 0.3s;
}
.operation-buttons .export-button:hover {
background-color: #803325;
cursor: pointer;
}
.operation-buttons .ship-details__button {
border: none;
background-color: #5a51d6;
color: white;
font-weight: bold;
padding: 10px;
-webkit-transition: background 0.3s;
transition: background 0.3s;
}
.operation-buttons .ship-details__button:hover {
background-color: #4f47bf;
cursor: pointer;
}
.operation-buttons .voyage-details__button {
border: none;
background-color: #88b33e;
color: white;
font-weight: bold;
padding: 10px;
-webkit-transition: background 0.3s;
transition: background 0.3s;
}
.operation-buttons .voyage-details__button:hover {
background-color: #739636;
cursor: pointer;
}
.ship-details__popup {
display: block;
visibility: hidden;
z-index: 9;
position: absolute;
top: 25%;
left: 20%;
width: 60vw;
background-color: lightgrey;
text-align: center;
padding: 1rem;
border: 1px solid black;
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-orient: vertical;
-webkit-box-direction: normal;
-ms-flex-direction: column;
flex-direction: column;
}
.ship-details__popup #ship-details__popup-closing-btn {
height: 20px;
width: 20px;
background-color: darkred;
color: white;
text-align: center;
font-weight: bold;
position: absolute;
right: 20px;
cursor: pointer;
border: 2px solid black;
}
.ship-details__popup form {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-orient: vertical;
-webkit-box-direction: normal;
-ms-flex-direction: column;
flex-direction: column;
width: 40%;
min-height: 25vh;
margin: 0 auto;
line-height: 1.5rem;
}
.ship-details__popup form input {
text-align: center;
}
.ship-details__popup select {
text-align-last: center;
padding: 0.1rem;
}
.ship-details__popup #ship-details-submit-btn {
border: none;
background-color: #31508f;
color: white;
font-weight: bold;
padding: 10px;
-webkit-transition: background 0.3s;
transition: background 0.3s;
}
.ship-details__popup #ship-details-submit-btn:hover {
background-color: darkred;
cursor: pointer;
}
.voyage-details__popup {
display: block;
visibility: hidden;
z-index: 9;
position: absolute;
top: 25%;
left: 20%;
width: 60vw;
background-color: lightgrey;
text-align: center;
padding: 1rem;
border: 1px solid black;
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-orient: vertical;
-webkit-box-direction: normal;
-ms-flex-direction: column;
flex-direction: column;
}
.voyage-details__popup form {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-orient: vertical;
-webkit-box-direction: normal;
-ms-flex-direction: column;
flex-direction: column;
width: 40%;
min-height: 25vh;
margin: 0 auto;
line-height: 1.5rem;
}
.voyage-details__popup form input {
text-align: center;
}
.voyage-details__popup #date-input {
padding-left: 15%;
cursor: pointer;
}
.voyage-details__popup #voyage-details__popup-closing-btn {
height: 20px;
width: 20px;
background-color: darkred;
color: white;
text-align: center;
font-weight: bold;
position: absolute;
right: 20px;
cursor: pointer;
border: 2px solid black;
}
.voyage-details__popup #voyage-details__submit-btn {
border: none;
background-color: #31508f;
color: white;
font-weight: bold;
padding: 10px;
-webkit-transition: background 0.3s;
transition: background 0.3s;
}
.voyage-details__popup #voyage-details__submit-btn:hover {
background-color: darkred;
cursor: pointer;
}
.new-record__popup {
display: block;
visibility: hidden;
z-index: 9;
position: absolute;
top: 25%;
left: 20%;
width: 60vw;
background-color: lightgrey;
text-align: center;
padding: 1rem;
border: 1px solid black;
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-orient: vertical;
-webkit-box-direction: normal;
-ms-flex-direction: column;
flex-direction: column;
top: 15%;
margin: auto;
}
.new-record__popup form {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-orient: vertical;
-webkit-box-direction: normal;
-ms-flex-direction: column;
flex-direction: column;
width: 40%;
min-height: 25vh;
margin: 0 auto;
line-height: 1.5rem;
}
.new-record__popup form input {
text-align: center;
}
.new-record__popup #new-record__popup-closing-btn {
height: 20px;
width: 20px;
background-color: darkred;
color: white;
text-align: center;
font-weight: bold;
position: absolute;
right: 20px;
cursor: pointer;
border: 2px solid black;
}
.new-record__popup #new-record__submit-btn {
border: none;
background-color: #31508f;
color: white;
font-weight: bold;
padding: 10px;
-webkit-transition: background 0.3s;
transition: background 0.3s;
width: 40%;
margin: auto;
}
.new-record__popup #new-record__submit-btn:hover {
background-color: darkred;
cursor: pointer;
}
.new-record__popup #edit-record {
border: none;
background-color: #31508f;
color: white;
font-weight: bold;
padding: 10px;
-webkit-transition: background 0.3s;
transition: background 0.3s;
width: 40%;
margin: auto;
display: none;
}
.new-record__popup #edit-record:hover {
background-color: darkred;
cursor: pointer;
}
.new-record__popup form {
width: auto;
display: -ms-grid;
display: grid;
-ms-grid-columns: 1fr 1fr;
grid-template-columns: 1fr 1fr;
grid-gap: 0.8rem;
-webkit-box-pack: justify;
-ms-flex-pack: justify;
justify-content: space-between;
line-height: 1.5rem;
padding-bottom: 2rem;
}
.new-record__popup form #new-record__remarks {
resize: none;
height: 3rem;
padding: 0.2rem;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Your ship's log book.</title>
<meta name="description" content="Electronic log book of your ship">
<link href="https://fonts.googleapis.com/css2?family=Montserrat&display=swap" rel="stylesheet">
</head>
<body>
<header>
<h1><span id="ship-type-span"></span><span id="ship-name-span">Ship's name</span> logbook</h1>
<div class="date-destination-div">
<p>Date:<span id="date-span"></span></p>
<p>
Voyage from: <span id="dest-span__from"></span><span id="dest-span__to"></span>
</p>
</div>
</header>
<div class="records-table-container">
<table class="records-table" sortable>
<thead>
<tr class=table__input-general>
<th colspan="5">Voyage details</th>
<th colspan="3">Weather conditions</th>
<th class="table__input-general-description" rowspan="2">General description / comments</th>
<th class="actions" rowspan="2">Edit / delete record</th>
</tr>
<tr class="table__input-details">
<td>Time <br> UTC</td>
<td>Latitude <br> [° , ']</td>
<td>Longitude <br> [° , ']</td>
<td>Heading <br> [°]</td>
<td>SOG <br>[kt]</td>
<!-- weather conditions -->
<td>Wind force</td>
<td>Sea state</td>
<td>Visibility</td>
</tr>
</thead>
</table>
</div>
<div class="operation-buttons">
<button class="new-record__btn">
Add new record
</button>
<button class="voyage-details__button">
Enter date and destination
</button>
<button class="ship-details__button">
Set ship's details
</button>
<button class="export-button">
Export to PDF
</button>
</div>
<div class="ship-details__popup">
<div id="ship-details__popup-closing-btn">X</div><br>
<h2>Enter your ship's name and type</h2>
<form>
<label for="ship-type">Select your ship's type</label>
<!-- Selection of ship type - to be modified upon submit -->
<select name="ship-type" id="ship-type">
<option value="M/V">Motor Vessel</option>
<option value="M/T">Motor Tanker</option>
<option value="S/V">Sailing Vessel</option>
<option value="S/Y">Sailing Yacht</option>
<option value="OSV">Offshore Support Vessel</option>
<option value="DSV">Dive Support Vessel</option>
<option value="PSV">Platform Supply Vessel</option>
<option value="SOV">Service Operation Vessel</option>
<option value="Tug">Tugboat</option>
</select>
<label for="ship-name">Enter your ship's name:</label>
<input type="text" id="ship-name-input" placeholder="Enter your ship's name here" required><br>
<button type="button" id="ship-details-submit-btn">Submit</button>
</form>
</div>
<!-- Pop up with date setting and destination -->
<div class="voyage-details__popup">
<div id="voyage-details__popup-closing-btn">X</div><br>
<h2>Fill out inputs below to update voyage details</h2>
<form>
<label for="date">Enter date:</label>
<input type="date" id="date-input" name="date" value="" min="2018-01-01" max="2021-01-01">
<label for="destination">Enter your last port of call:</label>
<input type="text" id="ship-destination-input__from" placeholder="Enter your last port of call">
<label for="destination">Enter your current destination:</label>
<input type="text" id="ship-destination-input__to" placeholder="Enter your current destination"><br>
<button type="button" id="voyage-details__submit-btn">Submit</button>
</form>
</div>
<!--New record pop up-->
<div class="new-record__popup">
<div id="new-record__popup-closing-btn">X</div><br>
<h2>Fill out inputs below to make a new record</h2>
<form autocomplete="off">
<label for="UTC-time">UTC Time </label>
<input type="text" name="UTC-time" placeholder="Enter time of the record" id="new-record__UTC-time">
<label for="latitude">Latitude [° , '] </label>
<div class="latitude-container">
<input type="text" name="latitude" placeholder="Enter latitude" id="new-record__latitude">
<select class="north-south">
<option value="N">N</option>
<option value="S">S</option>
</select>
</div>
<label for="longitude">Longitude [° , '] </label>
<div class="longitude-container">
<input type="text" name="longitude" placeholder="Enter longitude" id="new-record__longitude" title="Degrees and minutes">
<select class="east-west">
<option value="E">E</option>
<option value="W">W</option>
</select>
</div>
<label for="heading">Heading [°] </label>
<input type="text" name="heading" placeholder="Enter your heading" id="new-record__heading">
<label for="SOG">Speed Over Ground [kt] </label>
<input type="number" name="SOG" placeholder="Enter your speed" min="-5" max="40" id="new-record__SOG">
<label for="wind-force">Wind Force [B] </label>
<input type="number" name="wind-force" placeholder="Enter wind force" min="0" max="12" id="new-record__wind-force">
<label for="sea-state">Sea State </label>
<input type="number" name="sea-state" placeholder="Enter sea state" min="0" max="9" id="new-record__sea-state">
<label for="visibility">Visibility</label>
<input type="text" name="visibility" placeholder="Enter visibility" id="new-record__visibility">
<label for="remarks">General remarks </label>
<textarea name="remarks" name="remarks" id="new-record__remarks" placeholder="Add remarks..."></textarea>
</form>
<button type="button" id="new-record__submit-btn">Submit record</button>
<button type="button" id="edit-record">Edit record</button>
</div>
</body>
</html>
I have a simple section in which the user can add multiple buttons on click, I want these buttons names to be the editable meaning user can edit and save the button title as they wish.
Here is UI how it looks when use click add button
I want a user to be able to add button title by placing a mouse on enter button name.
When a user places a mouse on enter a button name a simple pop up text area will appear something like this.
$(document).ready(function() {
$('#btn').on('click', function() {
var buttonWithText = $("<div class='clickarea'>Enter button name</div>")
$(".main-container").append(buttonWithText);
})
})
.main-container {
height: 300px;
width: 400px;
background: red;
}
.clickarea {
height: 60px;
width: 50%;
/* margin: 20px; */
background: green;
display: flex;
justify-content: center;
align-items: center;
margin: 80px auto;
}
#btn {
cursor: pointer;
margin: 20px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="main-container">
</div>
<button id="btn">Add button</button>
For a very simple solution, you can use prompt:
$(document).ready(function() {
$('#btn').on('click', function() {
let text = prompt('enter button text:');
var buttonWithText = $("<div class='clickarea'>"+text+"</div>")
$(".main-container").append(buttonWithText);
})
})
.main-container {
height: 300px;
width: 400px;
background: red;
}
.clickarea {
height: 60px;
width: 50%;
/* margin: 20px; */
background: green;
display: flex;
justify-content: center;
align-items: center;
margin: 80px auto;
}
#btn {
cursor: pointer;
margin: 20px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="main-container">
</div>
<button id="btn">Add button</button>
If I understand correctly, you're desired functionality is something like below. You can achieve this by taking advantage of addEventListener and then creating a new button and adding it to the DOM on click:
EDIT:
I've updated my code based on your comment. I believe it now works according to the second scenario you have described. Let me know if this does not seem like the desired functionality.
function init() {
const buttonEl = document.querySelector('#button-el');
const buttonContainer = document.querySelector('#button-container');
const alertButtonText = e => alert(`You clicked: ${e.target.value}`);
const promptForText = e => {
const text = window.prompt('Enter button text');
if (text && text.trim().length) {
e.target.setAttribute('value', text);
e.target.removeEventListener('mouseover', promptForText);
e.target.addEventListener('click', alertButtonText);
}
}
buttonEl.addEventListener('click', e => {
const newButtonEl = document.createElement('input');
newButtonEl.setAttribute('type', 'button');
newButtonEl.addEventListener('mouseover', promptForText);
buttonContainer.appendChild(newButtonEl);
});
}
init();
#button-container>input {
height: 30px;
width: 40%;
background: green;
display: block;
align-items: center;
margin: 20px auto;
}
body {
background: red;
}
#button-el {
cursor: pointer;
margin: 20px;
}
<input id="button-el" type="button" value="Add button" />
<div id="button-container">
</div>
Wrote some code to Toggle whether a Grid of Images is Displayed or not. it is a 6x8 grid, so i did not include the 72 div elements here. When the Image Grid is not showing, a text list is visible. Hence the change in Button Text. Having trouble Finding any Errors.
<!DOCTYPE html>
<html>
<head>
<style>
#photo-index {
display: grid;
grid-template-columns: auto auto auto auto auto auto;
grid-gap: 0px;
background-color: #4f6b6f;
}
#toggle-button {
color: white;
background-color: rgba(0,0,0,.75);
border-radius: 3px;
text-align: center;
padding: 15px;
margin: auto;
position: absolute;
right: 5%;
cursor: pointer;
}
</style>
<script>
function toggleDisplay() {
var pi = document.getElementById('photo-index');
var displaySetting = pi.style.display;
var toggle-button = document.getElementById('toggle-button');
var showTaxonomy = 'Show Taxonomy';
var showImages = 'Show Images';
if (displaySetting == 'none') {
pi.style.display = 'grid';
toggle-button.innerHTML = showImages;
}
else {
pi.style.display = 'none';
toggle-button.innerHTML = showTaxonomy;
}
}
</script>
</head>
<body>
<div onclick="toggleDisplay()" id="toggle-button">Show Taxonomy</div>
<div id="photo-index">Large Grid of Stuff</div>
</body>
</html>
There is a error in the variable declaration toggle-button is not a valid variable in JavaScript, so use toggle_button. The JavaScript variable do not allow hyphen in variable declaration as it is considered as arithmetic operator.
function toggleDisplay() {
var pi = document.getElementById('photo-index');
var displaySetting = pi.style.display;
var toggle_button = document.getElementById('toggle-button');
var showTaxonomy = 'Show Taxonomy';
var showImages = 'Show Images';
if (displaySetting == 'none') {
pi.style.display = 'grid';
toggle_button.innerHTML = showImages;
}
else {
pi.style.display = 'none';
toggle_button.innerHTML = showTaxonomy;
}
}
#photo-index {
display: grid;
grid-template-columns: auto auto auto auto auto auto;
grid-gap: 0px;
background-color: #4f6b6f;
}
#toggle-button {
color: white;
background-color: rgba(0,0,0,.75);
border-radius: 3px;
text-align: center;
padding: 15px;
margin: auto;
position: absolute;
right: 5%;
cursor: pointer;
}
<div onclick="toggleDisplay()" id="toggle-button">Show Taxonomy</div>
<div id="photo-index">Large Grid of Stuff</div>