-2

I'm trying to insert a div into textarea:

$(document).ready(function(){
    $('#toclick').click(function(){
        var text = '<div id="block"></div>';
        $('#textarea').val( $('#textarea').val() + text);
    });
});

<textarea id="textarea"></textarea>
<div id="toclick"></div>

But there's only HTML inside textarea instead of div. How to fix it?

Joe
  • 15,205
  • 8
  • 49
  • 56
Antony Beard
  • 15
  • 1
  • 3

1 Answers1

0

Ok here goes nothing.

First, What you are trying to do, is not legal in HTML, I mean that is used while letting the users add stuff in HTML. Like tumblr. allows its users to. And at the same time, if you are not using any particular code to prevent mis use of that textarea then you might have to face some exceptions too. However, here is the code:

var text = "<div>Hello there</div>"  
$('input[name=my_input]').val(text);

What you were trying to do was wrong.. You add the input value as val('Hi there') not that way.

But still remember: its a textarea! Donot try to add images. Its meant for text. If you would like to add images, try using some iFrames or some divs.

Afzaal Ahmad Zeeshan
  • 15,669
  • 12
  • 55
  • 103