I'm trying to create a pass the message page but I'm unable to get the message the user inputs to be put on the page, with the current code I can see the text change for a split second then goes back to the original. When I try change the method of changing the text I then don't see any change at all.
const submit = document.getElementById('submitBtn');
let display = document.querySelector('.message-content');
let input = document.getElementById('message');
submit.addEventListener('click', function () {
let inputValue = input.value;
display.innerHTML = inputValue;
});
:root {
--lightBlue: #95b8d1;
--mainwhite: #f5f5f5;
--mainBlack: #333333;
}
.max-height {
min-height: 100vh;
}
body {
background: var(--lightBlue);
}
.message-container {
background: var(--mainwhite);
}
.message-content {
color: var(--lightBlue);
}
#submitBtn {
background: var(--lightBlue);
color: var(--mainwhite);
}
#submitBtn:hover {
color: var(--lightBlue);
color: var(--mainBlack);
}
.feedback {
display: none;
}
.show {
display: block;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="css/bootstrap.min.css">
<link rel="stylesheet" href="main.css">
<link href="https://fonts.googleapis.com/css?family=Courgette" rel="stylesheet">
<script src="all.js"></script>
<title>Starter Template</title>
<style>
</style>
</head>
<body>
<div >
<div >
<div >
<h4 >A messge you would like to pass</h4>
<form id="message-form">
<input type="text" name="" id="message" >
<input type="submit" id="submitBtn" >
</form>
<h5 >please enter a value to pass</h5>
<h4 >last message delivered</h4>
<h4 >hello world</h4>
</div>
</div>
</div>
<script src="js/jquery-3.3.1.min.js"></script>
<script src="js/bootstrap.bundle.min.js"></script>
<script src="app.js"></script>
</body>
</html>
CodePudding user response:
You want to cancel the default behaviour of the form when submitting.
const submit = document.getElementById('submitBtn');
let display = document.querySelector('.message-content');
let input = document.getElementById('message');
let form = document.getElementById('message-form');
form.addEventListener('submit', function(ev) {
let inputValue = input.value;
display.innerHTML = inputValue;
ev.preventDefault();
});
:root {
--lightBlue: #95b8d1;
--mainwhite: #f5f5f5;
--mainBlack: #333333;
}
.max-height {
min-height: 100vh;
}
body {
background: var(--lightBlue);
}
.message-container {
background: var(--mainwhite);
}
.message-content {
color: var(--lightBlue);
}
#submitBtn {
background: var(--lightBlue);
color: var(--mainwhite);
}
#submitBtn:hover {
color: var(--lightBlue);
color: var(--mainBlack);
}
.feedback {
display: none;
}
.show {
display: block;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="css/bootstrap.min.css">
<link rel="stylesheet" href="main.css">
<link href="https://fonts.googleapis.com/css?family=Courgette" rel="stylesheet">
<script src="all.js"></script>
<title>Starter Template</title>
<style>
</style>
</head>
<body>
<div >
<div >
<div >
<h4 >A messge you would like to pass</h4>
<form id="message-form">
<input type="text" name="" id="message" >
<input type="submit" id="submitBtn" >
</form>
<h5 >please enter a value to pass</h5>
<h4 >last message delivered</h4>
<h4 >hello world</h4>
</div>
</div>
</div>
<script src="js/jquery-3.3.1.min.js"></script>
<script src="js/bootstrap.bundle.min.js"></script>
<script src="app.js"></script>
</body>
</html>