Hi there,
This should do what you want:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Mirror Text</title>
<!--http://www.sitepoint.com/forums/showthread.php?970534-Not-sure-what-I-am-looking-for-->
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<style>
*{
margin:0;
padding:0;
}
h1{ margin-bottom:20px;}
h2{ margin-bottom:15px;}
p{ margin-bottom:10px;}
textarea{
width:99%;
margin-bottom:20px;
}
fieldset{
padding:8px;
margin-bottom:10px;
}
fieldset div:first-child{margin-bottom: 10px;}
#form, #preview{
width:46%;
float:left;
margin: 10px;
padding:10px;
border:solid 1px gray;
font-size:18px;
line-height:140%;
}
#form{ background:#CCC; }
.clear{ clear:both; }
</style>
</head>
<body>
<div id="form">
<h1>Input Area</h1>
<label for="header">Header:</label><br />
<textarea id="header" rows="5"></textarea>
<label for="content">Content:</label><br />
<textarea id="content" rows="15"></textarea>
<label for="footer">Footer:</label><br />
<textarea id="footer" rows="5"></textarea>
</div>
<div id="preview">
<h1>Preview Area</h1>
<fieldset>
<div>
<label for="bg">Select the background colour:</label>
<select class="selector" id="bg" data-property="background">
<option value="white">Default</option>
<option value="blue">Blue</option>
<option value="green">Green</option>
<option value="red">Red</option>
</select>
</div>
<div>
<label for="col">Select the background colour:</label>
<select class="selector" id="col" data-property="color">
<option value="black">Default</option>
<option value="yellow">Yellow</option>
<option value="white">White</option>
<option value="purple">Purple</option>
</select>
</div>
</fieldset>
<h2 id="headerPreview"></h2>
<p id="contentPreview"></p>
<p id="footerPreview"></p>
</div>
<script>
$(document).ready(function() {
function mirrorText(e, m){
e.keyup(function(){
m.text(e.val());
});
}
var pre = $("#preview");
$(".selector").change(function(){
pre.css( $(this).attr("data-property"), $(this).val());
});
mirrorText($("#header"),$("#headerPreview"));
mirrorText($("#content"),$("#contentPreview"));
mirrorText($("#footer"),$("#footerPreview"));
});
</script>
</body>
</html>
Please note that I have refactored the initial code I gave you, as it was querying the DOM every time a key was pressed.
Caching your selectors (like I have done here), is a much better practice.
HTH
Bookmarks