Jump to content

wrap selected text in tags


MDanz

Recommended Posts

this is the function i call onclick with..

 

<script type='text/javascript'>

function changeImage(){

 

  var newtext = "[/img]";

  document.changer.reply.value += newtext;

}

</script>

 

the textarea name is 'reply'

 

it puts the image tag in the textarea.. but how do i change it so it wraps around whatever is in the textbox.

Link to comment
https://forums.phpfreaks.com/topic/174571-wrap-selected-text-in-tags/
Share on other sites

<script type='text/javascript'>
function changeImage(){

   var newtext = "";
   document.changer.reply.value += newtext;
}
</script>

 

So, this code basically adds "" (IE: nothing) to the end of whatever is in reply? I'm not quite sure where this image tag is that you seem to be talking about.

 

*Side Note: In order to get images to wrap text, you use the align attribute.

 

http://www.w3schools.com/HTML/tryit.asp?filename=tryhtml_image_align

<script type='text/javascript'>
function changeImage(){

   var newtext = "";
   document.changer.reply.value += newtext;
}
</script>

 

So, this code basically adds "" (IE: nothing) to the end of whatever is in reply? I'm not quite sure where this image tag is that you seem to be talking about.

 

*Side Note: In order to get images to wrap text, you use the align attribute.

 

http://www.w3schools.com/HTML/tryit.asp?filename=tryhtml_image_align

 

oops.. the forum used the tags and displayed nothing

 

 

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();

 

<script type='text/javascript'>
function changeImage(){

   var newtext = "<img></img>";
   document.changer.reply.value += newtext;
}
</script>

 

how do i change the code so it wraps the tag around the selected text..

 

 

btw reply is the id of the text area

oops.. the forum used the tags and displayed nothing

 

 

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();

 

<script type='text/javascript'>
function changeImage(){

   var newtext = "<img></img>";
   document.changer.reply.value += newtext;
}
</script>

 

how do i change the code so it wraps the tag around the selected text..

 

 

btw reply is the id of the text area

 

First things first:

 

var reply = document.getElementById("reply");

reply.value += newtext;

 

Use this instead, at least you won't have issues trying to reference the textarea.

 

var text1 = "<img>";
var text2 = "</img>";

if (typeof(reply.selectionStart) != "undefined") { 
var begin = reply.value.substr(0, myArea.selectionStart); 
var selection = reply.value.substr(myArea.selectionStart, myArea.selectionEnd - myArea.selectionStart); 
var end = reply.value.substr(myArea.selectionEnd); 
reply.value = begin + text1 + selection + text2 + end; 
}

i tried your code... didn't work?? can you spot what is wrong?

 

 

onclick

<img src='http://www.u-stack.com/img.jpg' alt='image!' onclick='changeImage()' />

 

 

function

function changeImage(){

var text1 = "<img>";
var text2 = "</img>";

if (typeof(reply.selectionStart) != "undefined") {
var begin = reply.value.substr(0, myArea.selectionStart);
var selection = reply.value.substr(myArea.selectionStart, myArea.selectionEnd - myArea.selectionStart);
var end = reply.value.substr(myArea.selectionEnd);
reply.value = begin + text1 + selection + text2 + end;
}}

 

textarea

<textarea name="reply" cols="118" rows="15" wrap="hard" id="reply"  style="color:white; background-image:url(btalarge.jpg); border-color:#FBB917" readonly="readonly"></textarea

var text1 = "<img>";
var text2 = "</img>";

if (typeof(reply.selectionStart) != "undefined") {
var begin = reply.value.substr(0, reply.selectionStart);
var selection = reply.value.substr(reply.selectionStart, reply.selectionEnd - reply.selectionStart);
var end = reply.value.substr(reply.selectionEnd);
reply.value = begin + text1 + selection + text2 + end;
}

 

Try that.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.