Hi everyone I'm new in Web Development, even more in Javascript. I practice some Javascript to learn it in codepen.io, and I have an issue:
I've created an element with Javacript method createElement()
=> the element is a div. I've styled it => it's working
I also created a textarea in my HTML file with a button, and put the value of the <textarea>
into the div I created before (with Javascript).
The text/value (of the textarea) goes inside of the div (innerHTML) and it works fine for this part, but I want to apply some styles (with .style="{some styles goes here}"
, or the method classList.add("classX")
, but any of them is not working, like they all are ignored.
let div = document.createElement('div');
document.body.appendChild(div);
div.classList.add("test");
// Ici j'attrape l'INPUT
let inputText = document.querySelector("#txtarea");
//Ici j'atrappe le boutton => qui me permettra plus tard d'envoyer la valeur de ce que l'utilisateur aura écris dans l'input précédent au sen de la div à fond Jaune
let btn_send = document.querySelector("button");
//Processus d'envoi de la valeur
btn_send.addEventListener("click", function() {
// Variable me permettant de mettre la main sur la valeur de l'input
let value_style = inputText.value;
//Intégration du text avec InnerHTML dans la div
setTimeout
(function() {
div.innerHTML = value_style;
}, 1000);
// ajout de style au texte que l'on va envoyer dans la Div
//L'on vide l'input de sa valeur => pour renvoyer une nouvellle valeur
inputText.value = "";
})
* {
margin: 0;
box-sizing: border-box;
}
h1 {
background: red;
}
.test {
background-color: yellow;
height: 45vh;
width: 90%;
border: 1px outset black;
margin: 11vh auto;
}
.style {
text-align: center;
font-family: 'Helvetica', sans-serif;
color: #64a;
padding: 10px;
border: 1px solid blue;
background-color: #66e;
}
<!DOCTYPE html>
<html lang="fr">
<head>
<title>Test</title>
</head>
<meta charset="utf-8">
<body>
<header></header>
<footer></footer>
<label>Tapez quelque chose à afficher dans la div à fond jaune:</label>
<textarea id="txtarea" placeholder="..."></textarea>
<button>Appuyez pour envoyer</button>
</body>
</html>
Your .style
class isn't set onto anything. If you want to style the newly added text you would need to wrap it in something (in my example I wrapped it in a span
).
You could create the span
in the same way you did for the div
with a createElement
. In my example I just made it add a string into the innerHTML which will render as the element.
let div = document.createElement('div');
document.body.appendChild(div);
div.classList.add("test");
// Ici j'attrape l'INPUT
let inputText = document.querySelector("#txtarea");
//Ici j'atrappe le boutton => qui me permettra plus tard d'envoyer la valeur de ce que l'utilisateur aura écris dans l'input précédent au sen de la div à fond Jaune
let btn_send = document.querySelector("button");
//Processus d'envoi de la valeur
btn_send.addEventListener("click", function() {
// Variable me permettant de mettre la main sur la valeur de l'input
let value_style = inputText.value;
//Intégration du text avec InnerHTML dans la div
setTimeout
(function() {
div.innerHTML += `<span class="style">${value_style}</span>`;
}, 1000);
// ajout de style au texte que l'on va envoyer dans la Div
//L'on vide l'input de sa valeur => poir renvoyer une vouvellle valeur
inputText.value = "";
})
* {
margin: 0;
box-sizing: border-box;
}
h1 {
background: red;
}
.test {
background-color: yellow;
height: 45vh;
width: 90%;
border: 1px outset black;
margin: 11vh auto;
}
.style {
text-align: center;
font-family: 'Helvetica', sans-serif;
color: #64a;
padding: 10px;
border: 1px solid blue;
background-color: #66e;
}
<!DOCTYPE html>
<html lang="fr">
<head>
<title>Test</title>
</head>
<meta charset="utf-8">
<body>
<header>
</header>
<footer>
</footer>
<label>Tapez quelque chose à afficher dans la div à fond jaune:</label>
<textarea id="txtarea" placeholder="..."></textarea>
<button>Appuyez pour envoyer</button>
</body>
</html>