I want to display all of the user input from and unto a single line (with space between each) when I press a button. I already have the button working and it does display the user input, but not in the way I want it to.
How it displays:
one
two
three
How I want it to display:
one two three
All I find is how to display python, which I'm not even using. How should my code look like? (Btw, I'm not an expert at JavaScript or anything harder than that; got the JS from somewhere else)
<!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">
<link href="" />
<title>Document</title>
</head>
<body>
<form action="#">
<h1>Cert Form</h1>
<div>
<label for="typeOfCSR">Choose the type of...</label>
<select name="typeOfCSR" id="typeOfCSR">
<option value="">Select an Option </option>
<option value="One">One</option>
<option value="Two">Two</option>
<option value="Three">Three</option>
</select>
</div>
<div>
<label for="CN"> Enter the CN...</label>
<textarea cols="100" rows="1" id="CN"></textarea>
</div>
<div>
<label for="FQDN"> Enter the FQDN...</label>
<textarea cols="50" rows="5" id="FQDN"></textarea>
</div>
<div>
<label for="alternateName"> Enter the alt name</label>
<textarea cols="50" rows="5" id="altName"></textarea>
</div>
<div>
<label for="nameOfCert"> Enter the name...</label>
<textarea cols="50" rows="5" id="nameOfCert"></textarea>
</div>
</div>
<button type="button" id="review">Generate String</button>
<button type="submit"> Submit </button>
</form>
<br/>
<div id="result" style="border: 3px solid black;"> Result will show here</div>
<script>
const btn = document.getElementById('review');
btn.addEventListener('click',()=>{
let typeOfCSR = document.getElementById('typeOfCSR').value;
let CN = document.getElementById('CN').value;
let FQDN = document.getElementById('FQDN').value;
let altName = document.getElementById('altName').value;
let nameOfCert = document.getElementById('nameOfCert').value;
document.getElementById('result').innerHTML = typeOfCSR "<br/>" CN "<br/>" FQDN "<br/>" altName "<br/>" nameOfCert;
})
</script>
</body>
</html>
CodePudding user response:
You have <br/>
between each entry, which makes them show on separate lines, replace it with a space. And you should never use innerHTML
when dealing with user's input, use textContent
instead.
<!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">
<link href="" />
<title>Document</title>
</head>
<body>
<form action="#">
<h1>Cert Form</h1>
<div>
<label for="typeOfCSR">Choose the type of...</label>
<select name="typeOfCSR" id="typeOfCSR">
<option value="">Select an Option </option>
<option value="One">One</option>
<option value="Two">Two</option>
<option value="Three">Three</option>
</select>
</div>
<div>
<label for="CN"> Enter the CN...</label>
<textarea cols="100" rows="1" id="CN"></textarea>
</div>
<div>
<label for="FQDN"> Enter the FQDN...</label>
<textarea cols="50" rows="5" id="FQDN"></textarea>
</div>
<div>
<label for="alternateName"> Enter the alt name</label>
<textarea cols="50" rows="5" id="altName"></textarea>
</div>
<div>
<label for="nameOfCert"> Enter the name...</label>
<textarea cols="50" rows="5" id="nameOfCert"></textarea>
</div>
</div>
<button type="button" id="review">Generate String</button>
<button type="submit"> Submit </button>
</form>
<br/>
<div id="result" style="border: 3px solid black;"> Result will show here</div>
<script>
const btn = document.getElementById('review');
btn.addEventListener('click',()=>{
let typeOfCSR = document.getElementById('typeOfCSR').value;
let CN = document.getElementById('CN').value;
let FQDN = document.getElementById('FQDN').value;
let altName = document.getElementById('altName').value;
let nameOfCert = document.getElementById('nameOfCert').value;
document.getElementById('result').textContent = typeOfCSR " " CN " " FQDN " " altName " " nameOfCert;
})
</script>
</body>
</html>