Reputation: 21
I would like to add multiple lines of code to "myContent1" without having to replicate it as this can be quite tedious. Is there a more efficient way? Thanks!
function getCode(form){
myContent1 = document.inputForm.myContent1.checked;
output =
'<!DOCTYPE html>\n' +
'<html>\n' +
'<body>\n' +
((myContent1) ? '<div>content 1</div>' : '') + '\n' +
((myContent1) ? '<div>content 2</div>' : '') + '\n' +
'' +
'<\/body>\n' +
'<\/html>\n';
document.inputForm.source.value = output;
return output;
}
Upvotes: 2
Views: 317
Reputation: 7496
You can use ES6 Template literals to achieve the same.
This is a sample code. I have modified the code to check the conditions too.
getCode()
function getConditionalTemplate(x, y) {
if (Number(x) > Number(y)) {
return `<div>content1</div>`
} else {
return `<div>content2</div>`
}
}
function getCode() {
const myContent1 = document.getElementById('inputForm');
const x = 10000;
const y = 200;
const output =
`<!DOCTYPE html>
<html>
<body>
${getConditionalTemplate(`${x}`,`${y}`)}
<\/body>
<\/html>`
myContent1.innerHTML = output;
}
<div id="inputForm">
</div>
Upvotes: 2