|
| 1 | + |
| 2 | +<body> |
| 3 | + <div id="container"> |
| 4 | + <div id="box"> |
| 5 | + <p>This is a box with a border around it. |
| 6 | + Note which side of the box is |
| 7 | + red.</p> |
| 8 | + </div> |
| 9 | + </div> |
| 10 | + |
| 11 | + <div class="playable-buttons"> |
| 12 | + <input id="edit" type="button" value="Edit CSS" /> |
| 13 | + <input id="reset" type="button" value="Reset" /> |
| 14 | + </div> |
| 15 | + <textarea id="code" class="playable-code" rows="2">border-top-color: red;</textarea> |
| 16 | + |
| 17 | +<style> |
| 18 | + |
| 19 | +body { |
| 20 | + background-color: #F6F7F8; |
| 21 | + margin:3em auto; |
| 22 | + padding:1em; |
| 23 | +} |
| 24 | + |
| 25 | +textarea { |
| 26 | + width: 90%; |
| 27 | + height:auto; |
| 28 | + background-color: white; |
| 29 | + padding-right:16px; |
| 30 | +} |
| 31 | + |
| 32 | +#container { |
| 33 | + background-color: white; |
| 34 | + padding: 1em; |
| 35 | + width: 90%; |
| 36 | +} |
| 37 | +#box { |
| 38 | + border: solid 0.3em gold; |
| 39 | +} |
| 40 | +</style> |
| 41 | + |
| 42 | +<script> |
| 43 | +var box = document.getElementById("box"); |
| 44 | +var textarea = document.getElementById("code"); |
| 45 | +var reset = document.getElementById("reset"); |
| 46 | +var edit = document.getElementById("edit"); |
| 47 | +var code = textarea.value; |
| 48 | + |
| 49 | +function applyCode() { |
| 50 | + box.style.cssText = textarea.value; |
| 51 | +} |
| 52 | + |
| 53 | +reset.addEventListener("click", function() { |
| 54 | + textarea.value = code; |
| 55 | + applyCode(); |
| 56 | +}); |
| 57 | + |
| 58 | +function setSelection() { |
| 59 | + var start = textarea.value.indexOf(":") + 1; |
| 60 | + if ((textarea.value.length > start) && (textarea.value[start] === " ")) { |
| 61 | + start++; |
| 62 | + } |
| 63 | + |
| 64 | + var end = textarea.value.length - 1; |
| 65 | + if ((textarea.value.length > 0) && (textarea.value[end-1] === ";")) { |
| 66 | + end--; |
| 67 | + } |
| 68 | + |
| 69 | + textarea.setSelectionRange(start, end); |
| 70 | +} |
| 71 | + |
| 72 | +edit.addEventListener("click", function() { |
| 73 | + textarea.focus(); |
| 74 | + setSelection(); |
| 75 | +}); |
| 76 | + |
| 77 | +textarea.addEventListener("input", applyCode); |
| 78 | +window.addEventListener("load", applyCode); |
| 79 | +</script> |
| 80 | +</body> |
0 commit comments