How do you add particular text surrounding the text a user highlights.
Similar to how it's done in this form like if someone highlights
becomes ...Code:text
Code:[tag]text[/tag]
How do you add particular text surrounding the text a user highlights.
Similar to how it's done in this form like if someone highlights
becomes ...Code:text
Code:[tag]text[/tag]
The singleWrap function is used for inserting smileys.Code:functionsingleWrap(myField, myValue) { var obj = document.getElementById(myField); var start = obj.value.slice(0, obj.selectionStart); var end = obj.value.slice(obj.selectionEnd, obj.value.length); var text = start + myValue+' ' + end; document.getElementById(myField).value = text; } functionwrapHighlighted(myField, myValue){ var obj = document.getElementById(myField); var start = obj.value.slice(0, obj.selectionStart); var end = obj.value.slice(obj.selectionEnd, obj.value.length); var middle = obj.value.substring(obj.selectionStart, obj.selectionEnd); var text = start + '['+myValue+']' + middle + '[/'+myValue+'] ' + end; document.getElementById(myField).value = text; }
And the wrapHighlightedis used for wrapping 2 things around the highlighted.
Heres the bold tag in your case:
And to make a smiley:Code:<button type="button" onclick="wrapHighlighted('textarea','b')">B</button>
Code:<button type="button" onclick="singleWrap('textarea',': )')">: )</button>
Jeremy | jfein.net
This is great!
Is there a way to highlight the text and remove the tags as well like an undo.
Also, when you click the button, the button itself stays highlighted and selected. is there a way to remove the focus from the button after it's hit.
Hmm for undo I don't know - maybe create a function that makes an array of every full textarea value... What do you mean the button stays highlighted?
Jeremy | jfein.net
I mean the button has focus http://screencast.com/t/ZGNmZDFjZ
I found out today this code doesnt work in IE8 (not sure about previous versions.).
I had mozilla in my home computer.
In IE, when the function executes it basically highlights ALL text in the textfield (not what you highlight), copies and pastes it again and then surrounds it with the tags.
Bookmarks