Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jul 2008
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts

    adding things to a textarea

    hey im trying to create a simple text editor and im having a bit of trouble inserting things into a textarea. im no JS wiz, infact in quite the opposite.

    i did a bit of searching and i cam across this function:
    Code:
    //myField accepts an object reference, myValue accepts the text strint to add 
    function insertAtCursor(myField, myValue) { 
    //IE support 
    	if (document.selection) { 
    		myField.focus();
    
    //in effect we are creating a text range with zero 
    //length at the cursor location and replacing it 
    //with myValue 
    		sel = document.selection.createRange(); 
    		sel.text = myValue; 
    	}
    	//Mozilla/Firefox/Netscape 7+ support 
    	else if (myField.selectionStart ¦¦ myField.selectionStart == '0') 
    	{
    
    //Here we get the start and end points of the 
    //selection. Then we create substrings up to the 
    //start of the selection and from the end point 
    //of the selection to the end of the field value. 
    //Then we concatenate the first substring, myValue, 
    //and the second substring to get the new value. 
    		var startPos = myField.selectionStart; 
    		var endPos = myField.selectionEnd; 
    		myField.value = myField.value.substring(0, startPos)+ myValue+ myField.value.substring(endPos, myField.value.length); 
    	} else { 
    		myField.value += myValue; 
    	} 
    }
    it looks like i would expect the function i need to look and this is my source code:
    Code:
    <form name="addnews" action="/some/processing/script.php" method="post">
    	Title:<br />
    	<input type="text" name="title" /><br />
    	Message:<br />
    	<button type="button" onclick="insertAtCursor(document.getElementById('textarea'),'')">B</button><br />
    	<textarea id="textarea" name="news" rows="15" cols="50"></textarea><br />
    	<input type="submit" name="addnews" value="Add News" /> <a href="index.php">Cancel</a>
    	</form>
    the problem is that the text that i assume will be entered into the textarea is not appearing.
    am i doing something wrong?

    Thanks

  • #2
    Regular Coder Nile's Avatar
    Join Date
    Jun 2008
    Posts
    280
    Thanks
    2
    Thanked 46 Times in 46 Posts
    I tried running this script and using it correctly, it didn't work. So what I suggest is using this(me and a friend made it):
    Code:
    function singleWrap(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;
    }
    function wrapHighlighted(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;
    }
    The singleWrap function is used for inserting smileys.
    And the wrapHighlightedis used for wrapping 2 things around the highlighted.

    Heres the bold tag in your case:
    Code:
    <button type="button" onclick="wrapHighlighted('textarea','b')">B</button>
    And to make a smiley:
    Code:
    <button type="button" onclick="singleWrap('textarea',': )')">: )</button>
    This code is tested. So it should work.
    I hope this helps,
    Nile
    Last edited by Nile; 08-11-2008 at 08:12 PM.

  • #3
    New Coder
    Join Date
    Jul 2008
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts
    thats what i get for trusting google :P

    but that works fantastically!

    thanks alot!!

  • #4
    Regular Coder Nile's Avatar
    Join Date
    Jun 2008
    Posts
    280
    Thanks
    2
    Thanked 46 Times in 46 Posts
    I'm glad to help you.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •