jQuery addClass example
Change class name on click in jQuery
by Ben Clayton
HTML
<div id="banner-message">
<textarea id="mytext"><h1>Hello World</h1><h2>TWO</h2></textarea>
<div id="editor1">
</div>
<div id="editor2">
</div>
<button>Extract string and replace</button>
</div>
CSS
body {
background: #20262E;
padding: 20px;
font-family: Helvetica;
}
#mytext {
width: 80%;
height: 100px;
}
#banner-message {
background: #fff;
border-radius: 4px;
padding: 20px;
font-size: 25px;
text-align: center;
transition: all 0.2s;
margin: 0 auto;
width: 90%;
}
button {
background: #0084ff;
border: none;
border-radius: 5px;
padding: 8px 14px;
font-size: 15px;
color: #fff;
}
#banner-message.alt {
background: #0084ff;
color: #fff;
margin-top: 40px;
width: 200px;
}
#banner-message.alt button {
background: #fff;
color: #000;
}
JavaScript
// find elements
var $mytext = $("#mytext");
var $button = $("button");
var $editor1 = $("#editor1");
// handle click and add class
$button.on("click", function() {
var t = $mytext.val();
console.log(t);
$mytext.val(t + "<br>");
var html = "<textarea><h1>Hello World</h1></textarea>";
$editor1.html(html);
document.getElementById('editor2').innerHTML = html;
})