JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
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, 12:57 PM
cleaner416 cleaner416 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2002
Posts: 54 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 33 m 48 sec
Reputation Power: 7
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, 01: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: 295 GoMo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 23 h 27 m 21 sec
Reputation Power: 6
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, 01:29 PM
cleaner416 cleaner416 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2002
Posts: 54 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 33 m 48 sec
Reputation Power: 7
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, 01: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 19 h 32 m 47 sec
Reputation Power: 9
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, 03:36 PM
cleaner416 cleaner416 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2002
Posts: 54 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 33 m 48 sec
Reputation Power: 7
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, 06: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, 10:08 PM
Kravvitz's Avatar
Kravvitz Kravvitz is online now
CSS & JS/DOM Adept
Dev Shed God 23rd Plane (16000 - 16499 posts)
 
Join Date: Jul 2004
Location: USA
Posts: 16,008 Kravvitz User rank is General 8th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 8th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 8th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 8th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 8th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 8th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 8th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 8th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 8th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 8th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 8th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 8th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 8th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 8th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 8th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 8th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 3 Weeks 6 Days 3 h 22 m 28 sec
Reputation Power: 1544
__________________
Spreading knowledge, one newbie at a time.

Learn CSS. | PHP includes | X/HTML Validator | CSS validator | Dynamic Site Solutions

Design/program for Firefox (and/or Opera), apply fixes for IE, not the other way around.

I'm looking for new clients.

Reply With Quote
  #8  
Old June 15th, 2008, 04:19 PM
jannol jannol is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Location: Sweden
Posts: 6 jannol User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 33 m 12 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
Reply

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


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 | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT