JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsWeb DesignJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 8th, 2004, 11:57 AM
cleaner416 cleaner416 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2002
Posts: 56 cleaner416 User rank is Private First Class (20 - 50 Reputation Level)cleaner416 User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 3 h 48 m 6 sec
Reputation Power: 11
Add <b></b> tags to selected text in a textarea

I have a simple form like so:

PHP Code:
<form>
<
input type="button" value="BOLD" />
<
textarea rows="10" cols="30"></textarea>
<
input type="submit" value="SUBMIT" />
</
form


I would like to create some javascript to do the following - when the user clicks the "BOLD" button, the selected text gets a "<b>" and a "</b>" wrapped around it. Sort of like formatting text on this forum. (For some reason I can't see the vBulletin JS that's accomplishing the similar task)

Thanks in advance!

Reply With Quote
  #2  
Old December 8th, 2004, 12:12 PM
GoMo's Avatar
GoMo GoMo is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Location: London, UK
Posts: 299 GoMo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 32 m 6 sec
Reputation Power: 10
Send a message via MSN to GoMo
here's some code i've created to do the job on some of my sites, i'm not sure it's the best way to do it, but it does the job

PHP Code:
<script type="text/javascript">
<!--
    function 
formatText (tag) {
        var 
selectedText document.selection.createRange().text;
        
        if (
selectedText != "") {
            var 
newText "<" tag ">" selectedText "</" tag ">";
            
document.selection.createRange().text newText;
        }
    }
//-->
</script>

<form name="my_form">
    <textarea name="my_textarea"></textarea><br />
    <input type="button" value="bold" onclick="formatText ('b');" />
    <input type="button" value="italic" onclick="formatText ('i');" />
    <input type="button" value="underline" onclick="formatText ('u');" />
</form> 

goran

Reply With Quote
  #3  
Old December 8th, 2004, 12:29 PM
cleaner416 cleaner416 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2002
Posts: 56 cleaner416 User rank is Private First Class (20 - 50 Reputation Level)cleaner416 User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 3 h 48 m 6 sec
Reputation Power: 11
Thanks, Goran.

Unfortunately I could only get that code to work in IE. It fails in Firefox: 'document.selection' has no properties. I'll plug away a bit and see what I can come up with.

Reply With Quote
  #4  
Old December 8th, 2004, 12:45 PM
jerom jerom is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Posts: 1,014 jerom User rank is Corporal (100 - 500 Reputation Level)jerom User rank is Corporal (100 - 500 Reputation Level)jerom User rank is Corporal (100 - 500 Reputation Level)jerom User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 1 Day 21 h 56 m 52 sec
Reputation Power: 14
For Mozilla and friends:
Code:
  function formatText(el,tagstart,tagend) {
  	if (el.setSelectionRange) {
 		el.value = el.value.substring(0,el.selectionStart) + tagstart + el.value.substring(el.selectionStart,el.selectionEnd) + tagend + el.value.substring(el.selectionEnd,el.value.length)
  	}
  	else {
  		// IE code here...
  	}
  }
  ..
  <input type="button" value="BOLD" onclick="formatText(document.getElementById('myta'),'<b>','</b>')"/>
  <textarea id="myta" rows="10" cols="30"></textarea>
  <input type="submit" value="SUBMIT" />
  </form> 
  


Hope this helps,
Jeroen

Reply With Quote
  #5  
Old December 8th, 2004, 02:36 PM
cleaner416 cleaner416 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2002
Posts: 56 cleaner416 User rank is Private First Class (20 - 50 Reputation Level)cleaner416 User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 3 h 48 m 6 sec
Reputation Power: 11
Thanks - you rock.

Still doesn't work in Safari, but Google searches reveal that this is a limitation of Safarai (and Konqueror), which does not provide a programmatic way of inserting a cursor in a textarea.

Reply With Quote
  #6  
Old July 5th, 2007, 05:00 PM
Shamail Shamail is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2007
Posts: 1 Shamail User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 m 16 sec
Reputation Power: 0
BOLD doesn't appear as bold in textarea

I have been trying to look a wayaround to solve somewhat similar problem, but it seems like bold tag appears in coding and not word doesn't appear to be bold in the textarea. Please tell me does textarea does even support rich formatting??
is there any way around for this problem? how does all those web text editors work out there?

Quote:
Originally Posted by cleaner416
I have a simple form like so:

PHP Code:
<form>
<
input type="button" value="BOLD" />
<
textarea rows="10" cols="30"></textarea>
<
input type="submit" value="SUBMIT" />
</
form


I would like to create some javascript to do the following - when the user clicks the "BOLD" button, the selected text gets a "<b>" and a "</b>" wrapped around it. Sort of like formatting text on this forum. (For some reason I can't see the vBulletin JS that's accomplishing the similar task)

Thanks in advance!

Reply With Quote
  #7  
Old July 5th, 2007, 09:08 PM
Kravvitz's Avatar
Kravvitz Kravvitz is offline
CSS & JS/DOM Adept
Dev Shed God 30th Plane (19500 - 19999 posts)
 
Join Date: Jul 2004
Location: USA
Posts: 19,830 Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level) 
Time spent in forums: 6 Months 1 Day 16 h 39 m 44 sec
Reputation Power: 4192
__________________
Spreading knowledge, one newbie at a time. I'm available for hire at Dynamic Site Solutions.

Check out my blog. | Learn CSS. | PHP includes | X/HTML Validator | CSS validator | Common CSS Mistakes | Common JS Mistakes

Remember people spend most of their time on other people's sites (so don't violate web design conventions).

Reply With Quote
  #8  
Old June 15th, 2008, 03:19 PM
jannol jannol is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Location: Sweden
Posts: 8 jannol User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 5 m 15 sec
Reputation Power: 0
Send a message via ICQ to jannol
As of today, a year later after this post. The same code for mozilla browers does work in safari.

Reply With Quote
  #9  
Old July 16th, 2009, 05:14 AM
SARIARAVIND SARIARAVIND is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2009
Posts: 1 SARIARAVIND User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 m 10 sec
Reputation Power: 0
Thumbs up

Yea, it may not be the right way.....but it really works.....great

Can u pls tell hw it can a bold text can be made to non-bold text.
Thanx



Quote:
Originally Posted by GoMo
here's some code i've created to do the job on some of my sites, i'm not sure it's the best way to do it, but it does the job

PHP Code:
<script type="text/javascript">
<!--
    function 
formatText (tag) {
        var 
selectedText document.selection.createRange().text;
        
        if (
selectedText != "") {
            var 
newText "<" tag ">" selectedText "</" tag ">";
            
document.selection.createRange().text newText;
        }
    }
//-->
</script>

<form name="my_form">
    <textarea name="my_textarea"></textarea><br />
    <input type="button" value="bold" onclick="formatText ('b');" />
    <input type="button" value="italic" onclick="formatText ('i');" />
    <input type="button" value="underline" onclick="formatText ('u');" />
</form> 

goran

Reply With Quote
Reply

Viewing: Dev Shed ForumsWeb DesignJavaScript Development > Add <b></b> tags to selected text in a textarea

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap