PDA

View Full Version : onClick display code



chr15
12-19-2007, 07:41 AM
how do i make a form with checkboxes so that if you select one, a code will display in a textarea? but have the code disappear when you deselect that certain checkbox? thanks

TimFA
12-20-2007, 06:34 PM
<html>
<head>
<script type="text/javascript">
window.onload = function() {
var elem = document.getElementsByTagName('input')
for(var i=0;i<elem.length;i++) {
if(elem[i].type == "checkbox") {
elem[i].onclick = function() {
if(this.checked) {
try {
eval(this.getAttribute('onCheck'));
}
catch(e) {
alert("An error occurred when attempting\nto utilize the onCheck event ...")
}
}
else if(!this.checked) {
try {
eval(this.getAttribute('onUnCheck'));
}
catch(e) {
alert("An error occurred when attempting\nto utilize the onUnCheck event ...")
}
}
}
}
}
}
</script>
<script type="text/javascript">
function makeTxt(id,txt){
var obj = document.getElementById(id);
obj.firstChild?obj.firstChild.data=txt:obj.appendChild(document.createTextNode(txt))
}
</script>
</head>
<body>
<input onCheck="makeTxt('box', '&lt;html&gt;&nbsp;&lt;/html&gt;');" onUnCheck="makeTxt('box', '');" type="checkbox">
<div id="box"></div>
</body>
</html>


Works fine, but with inputs it will not change them at all, and with textareas it will only change them once, bizarre. Use of &lt;html&gt;&nbsp;&lt;/html&gt is the only way to do HTML with them.

&lt; = <
&gt; = >
&nbsp; = non-breaking space.

I'm no sure why I put in a non-breaking space rather than a normal just " " space, lol.

chr15
12-20-2007, 09:42 PM
thats the right idea but when i tried to add a second checkbox it wouldnt work lol. i think i should have been more specific - how do i multiple check boxes that will display a code (CSS) that will go in a textarea that will select the whole code with one click (unless you can do that with something other than a textarea?) but yea and if more than 1 check boxes are selected the codes will appear on top of eachother, and if they diselect a box that certain code goes away. thanks for your help btw

TimFA
12-20-2007, 11:51 PM
No problem to add another checkbox should be just cloning it, did you change the text of it? I just tried and it worked fine. If you mean that one will be there then it will be gone and a new one there, or do you mean that the newest one will go with the old one? Sorry, as usual I fell a bit sort, I cannot do this. The way I did it simply has a set text to insert, when you uncheck rather than removing what it put in, it deletes EVERYTHING inside the DIV. Same as when it goes in, it replaces whatever is currently there with "<html> </html>" sorry.