Jump to content

  • Log In with Google      Sign In   
  • Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

[web] Javascript - Add HTML tags around selection


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 InsaneBoarder234   Members   -  Reputation: 174

Like
0Likes
Like

Posted 27 June 2006 - 04:25 AM

I'm having difficulty finding suitable pages about this on google. Basically I'm writing a simple editor and I want to add a few buttons that will add tags around the current selection for formatting. So far I have this piece of javascript to apply bold to the selected piece of text.
function ApplyBold()
{
    theSelection = document.selection.createRange().text;
    if(theSelection)
    {
        document.selection.createRange().text = "<strong>" + theSelection + "</strong>";
        document.selection.anchorOffset = selStart;
    }
}

I have a few questions.. (1) How can I only affect the selection if it is within the textarea named 'text' on the form named 'editor'? (2) If there is no selection, or the 'text' textarea doesn't currently have keyboard focus, how can I detect this and use it to simply add "" into the textarea at the location of the cursor? (i.e. if I click at the start of the textarea, then click in another text box, then call ApplyBold() via a button, ApplyBold will insert the strong tag at the start of the 'text' textarea). Thanks!
Progress is born from the opportunity to make mistakes.

My prize winning Connect 4 AI looks one move ahead, can you beat it? @nickstadb

Ad:

#2 konForce   Members   -  Reputation: 584

Like
0Likes
Like

Posted 27 June 2006 - 07:32 AM

Try this:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>
<head>
<title>Untitled</title>

<script type="text/javascript">
function applyTag(obj, tag)
{
wrapText(obj, '<'+tag+'>', '</'+tag+'>');
};

function wrapText(obj, beginTag, endTag)
{
if(typeof obj.selectionStart == 'number')
{
// Mozilla, Opera, and other browsers
var start = obj.selectionStart;
var end = obj.selectionEnd;

obj.value = obj.value.substring(0, start) + beginTag + obj.value.substring(start, end) + endTag + obj.value.substring(end, obj.value.length);
}
else if(document.selection)
{
// Internet Explorer

// make sure it's the textarea's selection
obj.focus();
var range = document.selection.createRange();
if(range.parentElement() != obj) return false;

if(typeof range.text == 'string')
document.selection.createRange().text = beginTag + range.text + endTag;
}
else
obj.value += text;

};
</script>

</head>

<body>

<p>Blah blah blah.</p>

<textarea id="ta" rows="10" cols="50"></textarea> <br /><br />

<input type="button" value="Bold" onclick="applyTag(document.getElementById('ta'), 'b')" />
<input type="button" value="Italics" onclick="applyTag(document.getElementById('ta'), 'i')" />

</body>
</html>



#3 InsaneBoarder234   Members   -  Reputation: 174

Like
0Likes
Like

Posted 27 June 2006 - 08:22 AM

That works great, thanks!
Progress is born from the opportunity to make mistakes.

My prize winning Connect 4 AI looks one move ahead, can you beat it? @nickstadb




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS