Solved

Applying bold or italic or underline to a selection in a text area

Posted on 2009-04-14
4
1,289 Views
Last Modified: 2013-12-12
Hi,

I was wondering if it is possible to have an area of selected text be made bold (or italic/underline - i only need 1 but all 3 would be great) and then stored in a SQL database using a escape() string over AJAX.

Currently I have a from which calls a javascript function which then AJAX's an escape() string (to keep whitespaces).

I would like to be able to display the bold in 3 ways:

1. In the text area itself as it is being made (happy for this to be in tag form)

2. In the textarea when recalled for editing (again happy for this to be in tag form)

3. As actual bold text when displayed in a standard HTML page as I display my current output, simply as a paragraph.

I am not looking for a WYSIWYG style editor or anything like that, simply the option to add bold.  If there is an easy way to size as well that would be great but a bonus.
0
Comment
Question by:Vanq69
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 16

Accepted Solution

by:
sunithnair earned 500 total points
ID: 24136586
Try this code. This will add the necessary tags to the selected text inside a textarea. You can store them in database and later retrieve it which will automatically show up with tags when editing and formatted when shown in the browser as HTML.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<script type="text/javascript">
function formatText(el,tag)
{
    var selectedText = document.selection?document.selection.createRange().text:el.value.substring(el.selectionStart,el.selectionEnd);
    if(selectedText!='')
    {
        var newText='<'+tag+'>'+selectedText+'</'+tag+'>';
        el.value=el.value.replace(selectedText,newText)
    }
} 
</script>
</head>
<body>
<form action="" id="myForm">
<textarea name="myTextarea" rows="12" cols="50">This is just some sample text in the text area. You will be able to select some text here and click on the buttons below to add html tags to it</textarea><br>
<input type="button" value="Bold" onclick="formatText(myTextarea,'b')" />
<input type="button" value="Italic" onclick="formatText(myTextarea,'i')" />
<input type="button" value="Underline" onclick="formatText(myTextarea,'u')" />
</form>
</body>
</html> 

Open in new window

0
 

Author Comment

by:Vanq69
ID: 24136644
That is perfect, thank you so much.  Exactly what I wanted and worked easily.

Really is a credit when people are able to be so helpful.

Thank you.
0
 

Author Closing Comment

by:Vanq69
ID: 31569823
Brilliant assistance with working example.
0
 
LVL 16

Expert Comment

by:sunithnair
ID: 24136782
No Problem. The credit should also go to the person in this forum http://forums.devshed.com/javascript-development-115/textarea-bold-on-selected-area-512438.html. This is where I found the solution.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
Originally, this post was published on Monitis Blog, you can check it here . Websites are getting bigger and more complicated by the day. Video, images and custom fonts are all great for showcasing your product or service. But the price to pay in…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

742 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question