Writing inside a text area using Javascript

I try to write something in the text area when I click the link.

function writeText(txt){
    document.getElementById("writeArea").innerHTML = txt;
}

I would like to pass the html tag instead of txt as a parameter of this function so that I can get the image, link, etc. inside the text area. Is this possible in javascript? || Will jquery be good? Thanks in advance.

+3
source share
6 answers

If you want to display some images on the screen and anchor tags, then do not use the text box. Use any other container, for example <div>, <span>, <p>etc.

$("#yourelementid").html(yourtext);

will put the text (in your case HTML) inside the element with id yourelementid

HTML

<p id="para1"></p>

JQuery

var txt = "<a href='http://www.google.com'>Google</a>";
$("#para1").html(txt);

.

+3

, - jquery:

$('#writeArea').val(txt);
+4

value:

document.getElementById("writeArea").value = txt;
+2

jQuery, :

$("#yourid").val("hello");

, : http://jsfiddle.net/quQqH/

HTML- , (, div).

// Html
<div id="yourid"></div>

//JavaScript
$("#yourid").html('<a href="#">My link</a>');

, Rich Text Editor (, Yahoo), HTML- - . , , . :

var myEditor = new YAHOO.widget.SimpleEditor('yourid', {
    height: '200px',
    width: '350px',
    toolbar: 0 // Hides the toolbar
});
myEditor.render();


$("#yourid").val("Click for <a href='http://yahoo.com'>Yahoo</a>");

You can see how it works: http://jsfiddle.net/quQqH/1/ . In this case, I deleted the toolbar by setting it to 0, but it can be configured to display different buttons, etc. Just delete this line to display the default toolbar.

+2
source

just give it

$("#ID").val("<img src='images/logo.png' />");
0
source

If you want to write long text in a text field, you can use this method:

HTML

<textarea id="theId"></textarea>

JQuery

var a = 'Test textarea content. <a href="http://google.com">Google</a>" ';

$("#theId").val(a);

JS FIDDLE

0
source

Source: https://habr.com/ru/post/1778262/


All Articles