If a textarea exists in your document create a button at the top of it
If a textarea exists in your document, create a button at the top of it
Live Preview
If you will run above code, you will get below output.
To create a textarea in HTML, you can use the <textarea>
element. Here is an example of how you can use this element to create a textarea in your HTML document:
The rows
attribute specifies the number of rows that the textarea should have, and the cols
attribute specifies the number of columns that the textarea should have. The text between the opening and closing <textarea>
tags will be the default text that appears in the textarea when the page loads.
You can also use other attributes to customize the textarea, such as the name
attribute to give the textarea a name, the disabled
attribute to disable the textarea, and the readonly
attribute to make the textarea read-only.
Here is an example of a more advanced textarea element with additional attributes:
In this example, the name
attribute is used to give the textarea a name of "message", the placeholder
attribute is used to provide a hint to the user about what should be entered in the textarea, and the required
attribute is used to indicate that the textarea must be filled out before the form can be submitted.
Below section show how to create JavaScript code
Id of the textarea is: textAreaEditor
Remember that you have to add jQuery library.
var attr = $('#textAreaEditor').attr('id'); if (typeof attr !== 'undefined' && attr !== false) { $('#textAreaEditor').attr('rows', 15); var parentId = $('#textAreaEditor').parent().attr('id'); // alert(parentId); // alert("Attribute is present"); var r = $(''); const child = document.getElementById('textAreaEditor'); child.parentElement.classList.add('parenttextAreaEditor'); if(parentId){ $('#'+parentId).prepend(r); }else{ // alert("parentId"); $('.parenttextAreaEditor').prepend(r); } }
If you will click on the auto created button then this code will fire.
function setTextToCurrentPosParagraph() { var curPos = document.getElementById("textAreaEditor").selectionStart; console.log(curPos); let x = $("#textAreaEditor").val(); let text_to_insert = " Output:
"; $("#textAreaEditor").val(x.slice(0, curPos) + text_to_insert + '\r\n' + x.slice(curPos)); }