First I have created a <div>....</div>
which is hidden on pageload using javascript function. Then I have created an empty textbox using forms. Now when anyone clicks a button then the whole content of the hidden <div>....</div>
should be loaded in the textbox in the form. What should be code for this?
Adding the content of a to a textbox on click of a Button
Asked
Active
Viewed 2,913 times
0
Fabrício Matté
- 69,329
- 26
- 129
- 166
Hasan
- 99
- 1
- 2
- 7
2 Answers
2
maybe this way
<div id="hiddendiv"><strong>some</strong> content</div>
<button onclick="copyfunction();">click me</button>
<textarea id="textarea"></textarea>
<script>
function copyfunction() {
var textarea = document.getElementById('textarea');
var hiddendiv = document.getElementById('hiddendiv');
textarea.value = hiddendiv.innerText;
}
</script>
tested, it works
bukart
- 4,906
- 2
- 21
- 40
-
@bukfixart yeah no doubt it works, but the problem with this code is that it shows HTML syntax of the content in into the textbox but i want to display the composed form of it, i mean the outcome of the HTML code used and not the code itself
– Hasan
Oct 27 '12 at 19:38
-
ok, just edited the post, now it uses `innerText` instead of `innerHTML`
– bukart
Oct 27 '12 at 20:12
-
@bukfixart thank u very much!!...may i give you a little more trouble?
can you edit the code such that the textbox is also able to display any css styling done to the . I mean this code displays only the whole text but no effects. Thanks a lot once again.!
– Hasan
Oct 27 '12 at 22:37
-
sorry, this isn't possible with normal textareas. here you need `contendeditable` elements, like here: http://stackoverflow.com/questions/8956567/how-do-i-make-an-editable-div-look-like-a-text-field
– bukart
Oct 27 '12 at 22:41
Asked
Active
Viewed 2,913 times
0

Fabrício Matté
- 69,329
- 26
- 129
- 166

Hasan
- 99
- 1
- 2
- 7
2 Answers
2
maybe this way
<div id="hiddendiv"><strong>some</strong> content</div>
<button onclick="copyfunction();">click me</button>
<textarea id="textarea"></textarea>
<script>
function copyfunction() {
var textarea = document.getElementById('textarea');
var hiddendiv = document.getElementById('hiddendiv');
textarea.value = hiddendiv.innerText;
}
</script>
tested, it works

bukart
- 4,906
- 2
- 21
- 40
-
@bukfixart yeah no doubt it works, but the problem with this code is that it shows HTML syntax of the content ininto the textbox but i want to display the composed form of it, i mean the outcome of the HTML code used and not the code itself– Hasan Oct 27 '12 at 19:38
-
ok, just edited the post, now it uses `innerText` instead of `innerHTML` – bukart Oct 27 '12 at 20:12
-
@bukfixart thank u very much!!...may i give you a little more trouble? can you edit the code such that the textbox is also able to display any css styling done to the. I mean this code displays only the whole text but no effects. Thanks a lot once again.!– Hasan Oct 27 '12 at 22:37
-
sorry, this isn't possible with normal textareas. here you need `contendeditable` elements, like here: http://stackoverflow.com/questions/8956567/how-do-i-make-an-editable-div-look-like-a-text-field – bukart Oct 27 '12 at 22:41