mercredi 7 janvier 2015

function only works one time

This is my first question on here. I use this site all of the time, but have never posted on here.


When I call the function writeMessage(), when the checkbox is checked, the textarea is created. When I uncheck the checkbox, the textarea is removed. When I check the checkbox the second time, the function is not called. Any suggestions? I need for it to show the textarea any time the checkbox is checked, not just the first time around. Below is my code.


HTML:



<table>
<th>
<input type = "checkbox" name = "os0" id = "spaceForMsg" onClick = "writeMessage()">
<label for "gift"> Gift for someone</label>
</th>

</table>

<table >
<tr id = 'parent'>
<div id = 'printMsg'></div>
</tr>
</table>


javascript function:



function writeMessage() {

var x = document.getElementById('spaceForMsg');
var docBody = document.getElementById('parent');
var element = document.createElement('textarea');

element.cols='60';
element.rows='8';
element.id = 'msgArea';

if (x.checked) {
give = 'Type your gift message here: ';
docBody.appendChild(element);

} else {
give = '';
y = document.getElementById('parent').parentNode;
y.removeChild(y.childNodes[0]);

}

document.getElementById('printMsg').innerHTML = give;

}




Aucun commentaire:

Enregistrer un commentaire