i’m trying to create a javascript function which will append complex html to a div that is already in place (“complex” meaning more than a single element like
or etc..). i can’t place the whole div inside the () of .innerHTML(“div”) without it registering as an error because of all the symbols that cause JS to stop reading the content as a string. any recommendations? is there a way for me to create the element in my HTML file and hide it until it is triggered by a function in the javascript? thank you! (html and js below)
HTML:
<!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">
<title>Document</title>
<link rel="stylesheet" href= "style.css">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
</head>
<body>
<div class = "row r1">
<div class = "col-md-3"></div>
<div class = "col-md-6">
<h1>Your Password Generator</h1>
</div>
<div class = "col-md-3"></div>
</div>
<br><br><br>
<div class = "row r2">
<div class = "col-md-3"></div>
<div class = "col-md-6 hello">
<p>Hello, friend. Welcome to your new password generator. <br> Click "continue" to customize your password.</p>
</div>
<div class = "col-md-3"></div>
</div>
<br> <br>
<div class = "row r3">
<div class = "col-md-3"></div>
<div class = "col-md-6">
<div class = "cntrContent"></div>
</div>
<div class = "col-md-3"></div>
</div>
<div class = "characterSelect">
</div><div class = "text-center">
<div class="form-check">
<input class="form-check-input" type="checkbox" value="" id="defaultCheck1">
<label class="form-check-label" for="defaultCheck1">
Special Characters
</label> <br>
<input class="form-check-input" type="checkbox" value="" id="defaultCheck1">
<label class="form-check-label" for="defaultCheck1">
Numbers
</label> <br>
<input class="form-check-input" type="checkbox" value="" id="defaultCheck1">
<label class="form-check-label" for="defaultCheck1">
Letters
</label>
</div>
<br> <br>
<div class = "row r4">
<div class = "col-md-3"></div>
<div class = "col-md-6">
<div class= "text-center">
<button type="button" class="btn btn-light button">Continue</button>
</div>
</div>
<div class = "col-md-3"></div>
</div>
<!-- initial display: "Hello, friend. Welcome to your new password generator. Click "continue" to customize your password
then: new display, the question: "which kinds of characters would you like to include in your password?" followed by
list with checkboxes for "special character" "numbers" and "letters". this is followed by a "generate my password" button.
their selections will run through conditionals with event listeners. the next page will display the password under header "your password"
then a button below, "copy password to clipboard" and another button that says "i want another password" which brings
user back to customization options.-->
<script src="./script.js"></script>
JS:
var promptMsg = document.querySelector(".hello");
var cntrContent = document.querySelector(".cntrContent");
var button = document.querySelector(".button");
var characterSelect = document.querySelector(".characterSelect")
$(".button").on("click", function() {
$(promptMsg).text("What kinds of characters would you like to include in your password?");
$(characterSelect).innerHTML("THIS IS WHERE I AM TRYING TO APPEND THE DIV CLASSNAME 'characterSelect'")
})
You are mixing jquery methods with JS methods.
In your case, I recommend using template
<template id="myTemplate">
htmlto append
</template>
and then you can use append
.
With jquery:
var template = $("#myTemplate").html();
$(characterSelect).append(template);
With plain javascript:
var template = document.getElementById("myTemplate").content;
characterSelect.appendChild(template);
Here is a jsbin: https://jsbin.com/qucaxanohe/edit?html,js,output