英文:
Update input values into elements only JS
问题
我已创建了一个索引文件,以便在此处输入的信息添加到创建的元素中。这是用于评论部分的。
index.html
文件在这里,包括CSS和JavaScript。
<!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></title>
</head>
<body>
<div class="msg">
<div class="items"></div>
<div class="input">
<input type="text" class="name" placeholder="Name">
<input type="text" class="message" placeholder="Message">
<button id="button">Submit</button>
<button type="reset">Reset</button>
</div>
</div>
</body>
</html>
html, body {
padding: 0;
margin: 0;
}
.msg {
padding: 2em;
margin: 2em;
border-radius: 2vh;
height: 70vh;
display: flex;
align-items: center;
justify-content: left;
flex-direction: column;
background-color: #1e90ff;
}
.items {
height: 65vh;
overflow: scroll;
color: white;
width: 100%;
overflow-x: hidden;
margin: 10px;
}
input {
padding: 10px;
border: none;
border-radius: 8px;
outline: none;
font-size: 1em;
}
#button {
padding: 10px 20px;
border-radius: 8px;
border: none;
font-size: 1em;
}
.button {
padding: 10px 20px;
border-radius: 8px;
border: none;
font-size: 1em;
}
.message-item {
background-color: black;
padding: 1em;
border-radius: 8px;
margin: 3px;
}
let name = document.querySelector('.name').value;
let message = document.querySelector('.message').value;
let btn = document.getElementById('button');
let div = document.querySelector('.items')
btn.addEventListener('click', () => {
let item = document.createElement('div')
let inner = `
<h3>${name}</h3>
<p>${message}</p>
`
item.className = "message-item"
item.innerHTML = inner
div.append(item)
});
所以,我期望它会附加具有不同值的元素。例如,一旦我输入name为“harry”和message为“this is the message”,然后重置并输入另一个name和message,新创建的元素应该显示新输入的name和message。
英文:
I have created an index file so that the information entered here is added to the created element
this is for a review section
the index.html
file is here, and includes the CSS and js
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
let name = document.querySelector('.name').value;
let message = document.querySelector('.message').value;
let btn = document.getElementById('button');
let div = document.querySelector('.items')
btn.addEventListener('click', ()=>{
let item = document.createElement('div')
let inner = `
<h3>${name}</h3>
<p>${message}</p>
`
item.className = "message-item"
item.innerHTML = inner
div.append(item)
});
<!-- language: lang-css -->
html, body{
padding: 0;
margin: 0;
}
.msg{
padding: 2em;
margin: 2em;
border-radius: 2vh;
height: 70vh;
display: flex;
align-items: center;
justify-content: left;
flex-direction: column;
background-color: #1e90ff;
}
.items{
height: 65vh;
overflow: scroll;
color: white;
width: 100%;
overflow-x: hidden;
margin: 10px;
}
input{
padding: 10px;
border: none;
border-radius: 8px;
outline: none;
font-size: 1em;
}
#button{
padding: 10px 20px;
border-radius: 8px;
border: none;
font-size: 1em;
}
.button{
padding: 10px 20px;
border-radius: 8px;
border: none;
font-size: 1em;
}
.message-item{
background-color: black;
padding: 1em;
border-radius: 8px;
margin: 3px;
}
<!-- language: lang-html -->
<!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></title>
</head>
<body>
<div class="msg">
<div class="items"></div>
<div class="input">
<input type="text" class="name" placeholder="Name">
<input type="text" class="message" placeholder="Message">
<button id="button">Submit</button>
<button type="reset">Reset</button>
</div>
</div>
</body>
</html>
<!-- end snippet -->
So I am expecting it to append the elements which have different values
example once i enter the **name **"harry" and **message **as "this is the message"
and then i reset and enter another **name **and **message **then the newly created element should display the newly entered **name **and message
答案1
得分: 0
Your name
variable should be a pointer to the element, not the value.
Also, you should clear the input after adding.
英文:
Your name
variable should be a pointer to the element, not the value.
Also, you should clear the input after adding.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const
name = document.querySelector('.name'),
message = document.querySelector('.message'),
btn = document.getElementById('button'),
div = document.querySelector('.items');
const handleAdd = (e) => {
div.insertAdjacentHTML('beforeend', `
<div class="message-item">
<h3>${name.value}</h3>
<p>${message.value}</p>
</div>
`);
name.value = ''; // Clear name
message.value = ''; // Clear message
};
btn.addEventListener('click', handleAdd);
<!-- language: lang-css -->
html,
body {
padding: 0;
margin: 0;
}
.msg {
padding: 2em;
margin: 2em;
border-radius: 2vh;
height: 70vh;
display: flex;
align-items: center;
justify-content: left;
flex-direction: column;
background-color: #1e90ff;
}
.items {
height: 65vh;
overflow: scroll;
color: white;
width: 100%;
overflow-x: hidden;
margin: 10px;
}
input {
padding: 10px;
border: none;
border-radius: 8px;
outline: none;
font-size: 1em;
}
#button {
padding: 10px 20px;
border-radius: 8px;
border: none;
font-size: 1em;
}
.button {
padding: 10px 20px;
border-radius: 8px;
border: none;
font-size: 1em;
}
.message-item {
background-color: black;
padding: 1em;
border-radius: 8px;
margin: 3px;
}
<!-- language: lang-html -->
<div class="msg">
<div class="items"></div>
<div class="input">
<input type="text" class="name" placeholder="Name">
<input type="text" class="message" placeholder="Message">
<button id="button">Submit</button>
<button type="reset">Reset</button>
</div>
</div>
<!-- end snippet -->
A better approach
A better example would be to use a form. This way you can take advantage of built-in form validation, submission, and resetting.
For example, you can call elements by their name and you have the added bonus of <kbd>Enter</kbd> key support.
- Enter a name
- Press <kbd>Tab</kbd>
- Enter a message
- Press <kbd>Enter</kbd>
- The item is added
- The form is cleared
- Focus is sent to the name
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const handleAdd = (e) => {
e.preventDefault(); // Prevent page from navigating
const
form = e.target,
formElements = form.elements,
parent = form.closest('.msg'),
items = parent.querySelector('.items');
items.insertAdjacentHTML('beforeend', `
<div class="message-item">
<h3>${formElements.name.value}</h3>
<p>${formElements.message.value}</p>
</div>
`);
formElements.name.value = ''; // Clear name
formElements.message.value = ''; // Clear message
formElements.name.focus();
};
document.forms.namedItem('new-msg')
.addEventListener('submit', handleAdd);
<!-- language: lang-css -->
html,
body {
padding: 0;
margin: 0;
}
.msg {
padding: 2em;
margin: 2em;
border-radius: 2vh;
height: 70vh;
display: flex;
align-items: center;
justify-content: left;
flex-direction: column;
background-color: #1e90ff;
}
.items {
height: 65vh;
overflow: scroll;
color: white;
width: 100%;
overflow-x: hidden;
margin: 10px;
}
input {
padding: 10px;
border: none;
border-radius: 8px;
outline: none;
font-size: 1em;
}
.form-btn {
padding: 10px 20px;
border-radius: 8px;
border: none;
font-size: 1em;
}
.message-item {
background-color: black;
padding: 1em;
border-radius: 8px;
margin: 3px;
}
<!-- language: lang-html -->
<div class="msg">
<div class="items"></div>
<form id="new-msg" autocomplete="off">
<input type="text" name="name" placeholder="Name" required>
<input type="text" name="message" placeholder="Message">
<button type="submit" class="form-btn">Submit</button>
<button type="reset" class="form-btn">Reset</button>
</form>
</div>
<!-- end snippet -->
LocalStorage
Here is an example of local storage. The main idea here is how to store and restore the state of the messages.
const MESSAGES_KEY = "messages";
const main = () => {
// Restore all messages
const messageContainer = document.querySelector(".items");
__retrieveAllMessages().forEach((message) => {
insertMessage(message, messageContainer);
});
// Add event listener
document.forms.namedItem("new-msg").addEventListener("submit", handleAdd);
};
const saveMessage = (message) => {
__saveAllMessages(__retrieveAllMessages().concat(message));
};
const insertMessage = (message, container) => {
container.insertAdjacentHTML("beforeend", messageToHtml(message));
};
const messageToHtml = ({ name, message }) => `
<div class="message-item">
<h3>${name}</h3>
<p>${message}</p>
</div>
`;
const handleAdd = (e) => {
e.preventDefault(); // Prevent page from navigating
const form = e.target,
message = {
name: form.elements.name.value,
message: form.elements.message.value,
};
saveMessage(message);
insertMessage(message, form.closest(".msg").querySelector(".items"));
form.elements.name.value = ""; // Clear name
form.elements.message.value = ""; // Clear message
form.elements.name.focus();
};
const __retrieveAllMessages = () => {
return JSON.parse(localStorage.getItem(MESSAGES_KEY) ?? "[]");
};
const __saveAllMessages = (messages = []) => {
return localStorage.setItem(MESSAGES_KEY, JSON.stringify(messages));
};
main();
答案2
得分: 0
你需要在每次点击提交按钮时获取输入的值,而不仅仅是在页面加载时获取一次,并且在按下重置按钮时需要清除输入的值。
let btn = document.getElementById('button');
let reset = document.getElementById('reset');
let div = document.querySelector('.items');
btn.addEventListener('click', () => {
let name = document.querySelector('.name').value;
let message = document.querySelector('.message').value;
let item = document.createElement('div');
let inner = `
<h3>${name}</h3>
<p>${message}</p>
`;
item.className = "message-item";
item.innerHTML = inner;
div.append(item);
});
reset.addEventListener('click', () => {
document.querySelector('.name').value = '';
document.querySelector('.message').value = '';
});
CSS和HTML部分不需要翻译。
英文:
You need to get the values of your input on every button submit button press, not just once on page load, and also you need to clear your inputs when you press the reset button
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
let btn = document.getElementById('button');
let reset = document.getElementById('reset');
let div = document.querySelector('.items')
btn.addEventListener('click', () => {
let name = document.querySelector('.name').value;
let message = document.querySelector('.message').value;
let item = document.createElement('div')
let inner = `
<h3>${name}</h3>
<p>${message}</p>
`
item.className = "message-item"
item.innerHTML = inner;
div.append(item)
});
reset.addEventListener('click', () => {
let name = document.querySelector('.name').value = '';
let message = document.querySelector('.message').value = '';
});
<!-- language: lang-css -->
html, body{
padding: 0;
margin: 0;
}
.msg{
padding: 2em;
margin: 2em;
border-radius: 2vh;
height: 70vh;
display: flex;
align-items: center;
justify-content: left;
flex-direction: column;
background-color: #1e90ff;
color: white;
}
.items{
height: 65vh;
overflow: scroll;
color: white;
width: 100%;
overflow-x: hidden;
margin: 10px;
}
input{
padding: 10px;
border: none;
border-radius: 8px;
outline: none;
font-size: 1em;
}
#button{
padding: 10px 20px;
border-radius: 8px;
border: none;
font-size: 1em;
}
.button{
padding: 10px 20px;
border-radius: 8px;
border: none;
font-size: 1em;
}
.message-item{
background-color: black;
padding: 1em;
border-radius: 8px;
margin: 3px;
}
<!-- language: lang-html -->
<!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></title>
</head>
<body>
<div class="msg">
<div class="items"></div>
<div class="input">
<input type="text" class="name" placeholder="Name">
<input type="text" class="message" placeholder="Message">
<button id="button">Submit</button>
<button id="reset">Reset</button>
</div>
</div>
</body>
</html>
<!-- end snippet -->
答案3
得分: -1
你可以从输入中获取值,但它们在填充之前就是字符串值,不会在输入更改时动态更新。
我将这些代码移到了你的函数内部,这样每次点击按钮时都会获取到最新的数据。
<!-- 开始代码段: js 隐藏: false 控制台: true Babel: false -->
<!-- 语言: lang-js -->
let btn = document.getElementById('button');
btn.addEventListener('click', () => {
let name = document.querySelector('.name').value;
let message = document.querySelector('.message').value;
let div = document.querySelector('.items')
let item = document.createElement('div')
let inner = `
<h3>${name}</h3>
<p>${message}</p>
`
item.className = "message-item"
item.innerHTML = inner
div.append(item)
});
<!-- 语言: lang-css -->
html, body{
padding: 0;
margin: 0;
}
.msg{
padding: 2em;
margin: 2em;
border-radius: 2vh;
height: 70vh;
display: flex;
align-items: center;
justify-content: left;
flex-direction: column;
background-color: #1e90ff;
}
.items{
height: 65vh;
overflow: scroll;
color: white;
width: 100%;
overflow-x: hidden;
margin: 10px;
}
input{
padding: 10px;
border: none;
border-radius: 8px;
outline: none;
font-size: 1em;
}
#button{
padding: 10px 20px;
border-radius: 8px;
border: none;
font-size: 1em;
}
.button{
padding: 10px 20px;
border-radius: 8px;
border: none;
font-size: 1em;
}
.message-item{
background-color: black;
padding: 1em;
border-radius: 8px;
margin: 3px;
}
<!-- 语言: lang-html -->
<!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></title>
</head>
<body>
<div class="msg">
<div class="items"></div>
<div class="input">
<input type="text" class="name" placeholder="Name">
<input type="text" class="message" placeholder="Message">
<button id="button">Submit</button>
<button type="reset">Reset</button>
</div>
</div>
</body>
</html>
<!-- 结束代码段 -->
英文:
You get the values from your inputs before they are filled. They are just string values and do not update dynamically when the input changes.
I moved these lines to inside your function so that you get the most recent data every time you click the button.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
let btn = document.getElementById('button');
btn.addEventListener('click', ()=>{
let name = document.querySelector('.name').value;
let message = document.querySelector('.message').value;
let div = document.querySelector('.items')
let item = document.createElement('div')
let inner = `
<h3>${name}</h3>
<p>${message}</p>
`
item.className = "message-item"
item.innerHTML = inner
div.append(item)
});
<!-- language: lang-css -->
html, body{
padding: 0;
margin: 0;
}
.msg{
padding: 2em;
margin: 2em;
border-radius: 2vh;
height: 70vh;
display: flex;
align-items: center;
justify-content: left;
flex-direction: column;
background-color: #1e90ff;
}
.items{
height: 65vh;
overflow: scroll;
color: white;
width: 100%;
overflow-x: hidden;
margin: 10px;
}
input{
padding: 10px;
border: none;
border-radius: 8px;
outline: none;
font-size: 1em;
}
#button{
padding: 10px 20px;
border-radius: 8px;
border: none;
font-size: 1em;
}
.button{
padding: 10px 20px;
border-radius: 8px;
border: none;
font-size: 1em;
}
.message-item{
background-color: black;
padding: 1em;
border-radius: 8px;
margin: 3px;
}
<!-- language: lang-html -->
<!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></title>
</head>
<body>
<div class="msg">
<div class="items"></div>
<div class="input">
<input type="text" class="name" placeholder="Name">
<input type="text" class="message" placeholder="Message">
<button id="button">Submit</button>
<button type="reset">Reset</button>
</div>
</div>
</body>
</html>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论