|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| ||||||||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Javascript - Help changing code
this isn't the full code.. i'm not including the class with all the bbtags
here is how the function for image tag is called with onclick=changeImage(); Code:
<script type='text/javascript'>
function changeImage(){
var newtext = "<img></img>";
document.changer.reply.value += newtext;
}
</script>
how do i change it so whatever the selected text is it wraps the tag around it? .. like on this forum |
|
#2
|
|||
|
|||
|
you can use string.selectionStart and string.selectionEnd to get the start and end positions of a selection then use substring to chop the string up and get the before selection, selection and after selection then put them back together with the tag around the selection. here is a simple example.
Code:
<html>
<head>
<script type='text/javascript'>
var doSomething = function(){
var ele = document.getElementById("test");
var tagStart = '<b>';
var tagEnd = '</b>';
if(ele.selectionStart==ele.selectionEnd){
//nothing selected put the tag at the end of the textarea
ele.value += tagStart+tagEnd;
} else {
ele.value = ele.value.substring(0, ele.selectionStart)
+tagStart
+ele.value.substring(ele.selectionStart, ele.selectionEnd)
+tagEnd
+ele.value.substring(ele.selectionEnd);
}
}
</script>
</head>
<body>
<textarea id="test">this is some text</textarea>
<input type='button' onclick='doSomething();' id='submitButton' value='GO' />
</body>
</html>
|
![]() |
| Viewing: Codewalkers Forums > Other Technologies > Client Side Things > Javascript - Help changing code |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|