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
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

Link to comment
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

 

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

Link to comment
Share on other sites

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; 
}

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.