troubleshooting Question

JavaScript textarea insert a paragraph

Avatar of jonatec
jonatecFlag for United Kingdom of Great Britain and Northern Ireland asked on
Web Languages and StandardsJavaScriptHTML
4 Comments1 Solution899 ViewsLast Modified:
Hi. I have a HTML <textarea. I want to be able to click and position the cursor just after the word "growth." in the text to create a paragraph. Then using JavaScript to 1. insert a <p> tag at the cursor position, then 2. copy the html text from the <textarea tag to the paragraph tag <p1 (id='p1'). So that the user can see the HTML inserted into the <textarea and also review the change in the <p> below. Hope that makes sense. I know how to use the createRange method for selected text but don't know how to insert a paragraph, (or line break), at the cursor position. Can you help please? My code is below:

<html>
<head>
<style>
#secRtf {border:solid 1px black; width:380px;padding:10px; height:145px }
</style>
<script language='javascript'>
function fnPara()
{
}
</script>
</head>
<body>
<form>
  <textarea id='txtBox' name='txtBox' cols=45 rows=10>More than half the rain that falls on this catchment is lost to evaporation
and plant growth. The remainder provides the water resource that has to be
shared between river flows, to support the natural environment, navigation and
the community needs for water supplies to homes, industry and agriculture.</textarea>
  <p>
  <input type='button' value='Para' onclick='fnPara()'>
  <p>
  <div id='secRtf'>
    <p id='p1' style='font-family:verdana'></p>
  </div
</form>
</body>
</html>
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 4 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 4 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros