Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
Building a WYSIWYG Editor - Small question
Old 07-24-2010, 01:09 PM Building a WYSIWYG Editor - Small question
Physicsguy's Avatar
404 - Title not found

Posts: 919
Name: Scott Kaye
Location: Ontario
Trades: 0
Just like this one.

Now this text editor I am typing this in is not a textarea. I suspect it's a div with contenteditable="true" and displaymode="on".

I can make a div like this one and all, but my problem comes when I try to create buttons that do things to selected text in the box.

For example, if I select the words 'For example', and go up and click the B button, it will make the text bold. All it's doing is putting <b> tags around the selected text. Easy as that.

Or is it?

I've tried a bunch of scripts, and I've managed to get the contents of my selection, but I can't actually do anything to it, like set 'selTxt.style.font-weight='700';' or anything :\.

I'm not great at JS, and I am wondering if somebody could point me in the right direction into making something like this happen.


I've thought about this a fair bit and have tried the JS replace() method, but grabbing the selected text and replacing it with <b>selTxt</b> but for some strange reason it doesn't decide to work. Anyway here's the code:

HTML Code:
<script language="javascript">
function getSelText()
{
    var editor = document.getElementById('editor');
    var txt = '';
     if (window.getSelection)
    {
        txt = window.getSelection();
             }
    else if (document.getSelection)
    {
        txt = document.getSelection();
            }
    else if (document.selection)
    {
        txt = document.selection.createRange().text;
            }
    else return;
    editor.innerHTML = "<b>"+txt+"</b>";
}
</script>
<input type="button" value="Make Bold" onmousedown="getSelText()"> 

<div id='editor' onclick="javascript:this.designMode='on';" designmode="on" contenteditable="true">Editor Box</div>
As you can see, I'm grabbing any text that is selected and setting the editor's innerHTML to that text that is selected, and making it bold. (All I'm focusing on is bold right now). But it deletes everthing currently in the editor, and sets it to my selection; bold.

Long story short, how do I make just the text I selected bold. Just bold, no other fancy stuff.

Thanks

-PG
__________________
Check out my
Please login or register to view this content. Registration is FREE
or my
Please login or register to view this content. Registration is FREE
!
Physicsguy is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 07-24-2010, 01:33 PM Re: Building a WYSIWYG Editor - Small question
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,385
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Quote:
I suspect it's a div with contenteditable="true" and displaymode="on".
Internet Exploder ONLY "features", so probably not.

BTW are you refering to the "Quick Reply" or the "Advanced reply" at this very forum?
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 07-24-2010, 01:39 PM Re: Building a WYSIWYG Editor - Small question
Physicsguy's Avatar
404 - Title not found

Posts: 919
Name: Scott Kaye
Location: Ontario
Trades: 0
Never mind, I found something out, and now it works

Here's the code:

HTML Code:
<script language="javascript">
function format()
{
    var editor = document.getElementById('editor');
    var txt = '';
    var output = document.getElementById('tbYEAH');
     if (window.getSelection)
    {
        txt = window.getSelection();
             }
    else if (document.getSelection)
    {
        txt = document.getSelection();
            }
    else if (document.selection)
    {
        txt = document.selection.createRange().text;
            }
    else return;
    matched = editor.innerHTML.match(txt);
    editor.innerHTML = editor.innerHTML.replace(matched,"<b>"+matched+"</b>");
}
</script>
<input type="button" value="Make Bold" onmousedown="format()"> 
<div id='editor' onclick="javascript:this.designMode='on';" designmode="on" contenteditable="true">Edit Box</div>
And I was talking about the Advanced Reply.

BTW Chrishirst, contenteditable and designmode aren't strictly related to IE, they're working fine right now in Firefox.
__________________
Check out my
Please login or register to view this content. Registration is FREE
or my
Please login or register to view this content. Registration is FREE
!

Last edited by Physicsguy; 07-24-2010 at 01:41 PM..
Physicsguy is offline
Reply With Quote
View Public Profile
 
Old 07-24-2010, 01:39 PM Re: Building a WYSIWYG Editor - Small question
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,385
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
But however, would something like this be what you are looking for?
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 07-24-2010, 01:41 PM Re: Building a WYSIWYG Editor - Small question
Physicsguy's Avatar
404 - Title not found

Posts: 919
Name: Scott Kaye
Location: Ontario
Trades: 0
Nah, that inserts text. I'm looking to replace it. Thanks though

Hmm... I'm having some trouble making it Not Bold. I can make it bold, but if it's already bold it can't make it bolder :P.

Is there a way to detect if there are <b></b> tags around the selected text?
__________________
Check out my
Please login or register to view this content. Registration is FREE
or my
Please login or register to view this content. Registration is FREE
!

Last edited by Physicsguy; 07-24-2010 at 01:47 PM..
Physicsguy is offline
Reply With Quote
View Public Profile
 
Old 07-24-2010, 02:07 PM Re: Building a WYSIWYG Editor - Small question
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,385
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Ah but what you don't see is IF there is text selected in the textarea the insert function WILL replace it with whatever you pass to it.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 07-24-2010, 02:10 PM Re: Building a WYSIWYG Editor - Small question
Physicsguy's Avatar
404 - Title not found

Posts: 919
Name: Scott Kaye
Location: Ontario
Trades: 0
Ah, you're right, I didn't see that, cool!

Does anybody know a fix to my question above? So far I have nothing
__________________
Check out my
Please login or register to view this content. Registration is FREE
or my
Please login or register to view this content. Registration is FREE
!
Physicsguy is offline
Reply With Quote
View Public Profile
 
Old 07-29-2010, 07:35 PM Re: Building a WYSIWYG Editor - Small question
Physicsguy's Avatar
404 - Title not found

Posts: 919
Name: Scott Kaye
Location: Ontario
Trades: 0
...I know...
__________________
Check out my
Please login or register to view this content. Registration is FREE
or my
Please login or register to view this content. Registration is FREE
!
Physicsguy is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Building a WYSIWYG Editor - Small question
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.26410 seconds with 12 queries