Web Design and Web Development Forum

  1. #1
    Join Date
    Jul 2007
    Location
    England
    Posts
    55
    Rep Power
    5
  2. xssvgamer is on a distinguished road
  3. onClick insert into a textbox

    ok you know on forums where you click the bb code sign for bold and it inserts bold tags into the textbox, well currently im using :
    Code:
    onClick="document.form.input name.value+=''
    but when i use it it always inserts it at the end of the text and id like it to go around the highlighted text or atleast where the cursor is


    any help please?
    Reply With Quote Reply With Quote
  4. #2
    Join Date
    Oct 2004
    Age
    20
    Posts
    1,967
    Rep Power
    9
  5. Reaper is on a distinguished road
  6. Re: onClick insert into a textbox

    Code:
    this.wrap_tags = function(tagname, useoption, selection)
    	{
    		tagname = tagname.toUpperCase();
    
    		switch (tagname)
    		{
    			case 'CODE':
    			case 'HTML':
    			case 'PHP':
    			{
    				this.apply_format('removeformat');
    			}
    			break;
    		}
    
    		if (typeof selection == 'undefined')
    		{
    			selection = this.get_selection();
    			if (selection === false)
    			{
    				selection = '';
    			}
    			else
    			{
    				selection = new String(selection);
    			}
    		}
    
    		if (useoption === true)
    		{
    			var option = this.show_prompt(construct_phrase(vbphrase['enter_tag_option'], ('[' + tagname + ']')), '');
    			if (option = this.verify_prompt(option))
    			{
    				var opentag = '[' + tagname + '="' + option + '"' + ']';
    			}
    			else
    			{
    				return false;
    			}
    		}
    		else if (useoption !== false)
    		{
    			var opentag = '[' + tagname + '="' + useoption + '"' + ']';
    		}
    		else
    		{
    			var opentag = '[' + tagname + ']';
    		}
    
    		var closetag = '[/' + tagname + ']';
    		var text = opentag + selection + closetag;
    
    		this.insert_text(text, opentag.vBlength(), closetag.vBlength());
    
    		return false;
    	};
    And heres the insert_text function:
    Code:
    		this.insert_text = function(text, movestart, moveend)
    		{
    			this.check_focus();
    
    			if (typeof(this.editdoc.selection) != 'undefined' && this.editdoc.selection.type != 'Text' && this.editdoc.selection.type != 'None')
    			{
    				movestart = false;
    				this.editdoc.selection.clear();
    			}
    
    			var sel = this.editdoc.selection.createRange();
    			sel.pasteHTML(text);
    
    			if (text.indexOf('\n') == -1)
    			{
    				if (movestart === false)
    				{
    					// do nothing
    				}
    				else if (typeof movestart != 'undefined')
    				{
    					sel.moveStart('character', -text.vBlength() +movestart);
    					sel.moveEnd('character', -moveend);
    				}
    				else
    				{
    					sel.moveStart('character', -text.vBlength());
    				}
    				sel.select();
    			}
    		};
    I think that is what this forum uses at the moment. I am not positive, this was what I got from a quick scan.

    Of course this is vBulletin's, and it may be illegal to use, and not all those functions inside might need to be used but you can quickly search http://youngcoders.com/clientscript/...tedit.js?v=363 for it.
    Last edited by Reaper; 07-19-2007 at 02:06 PM.
    Reply With Quote Reply With Quote
  7. #3
    Join Date
    Feb 2005
    Location
    Moodles, UK
    Posts
    2,258
    Rep Power
    0
  8. Calamitie will become famous soon enough
  9. Re: onClick insert into a textbox

    I was looking to do exactly that and the following code works perfectly (tested in Microsoft Internet Explorer 7 & Mozilla Firefox 2):

    Code:
    <html>
    
    <head>
    <title>Vitrone text editor snippets</title>
    <style type="text/css">
    #bold_button {
    	background: url('');
    }
    </style>
    <script type="text/javascript">
    function insertFormatting(BBcode) {
    	textarea = document.getElementById('text');
    	if (document.selection) {
    		textarea.focus();
    		selected = document.selection.createRange();
    		selected.text = "["+BBcode+"]" + selected.text + "[/"+BBcode+"]";
    	} else if (textarea.selectionStart || textarea.selectionStart == '0') {
    		var start = textarea.selectionStart;
    		var end = textarea.selectionEnd;
    		textarea.value = textarea.value.substring(0, start) + "["+BBcode+"]" + textarea.value.substring(start, end) + "[/"+BBcode+"]" + textarea.value.substring(end, textarea.value.length);
    	}
    	return true;
    }
    </script>
    </head>
    
    <body>
    <p><button id="bold_button" onclick="insertFormatting('b')">B</button></p>
    <p><textarea id="text">Hello there moo cow.</textarea></p>
    </body>
    
    </html>
    Hope that helps :D

    P.S. that's not illegal to use just in case you're wondering ;)
    Reply With Quote Reply With Quote
  10. #4
    Join Date
    Jul 2007
    Location
    England
    Posts
    55
    Rep Power
    5
  11. xssvgamer is on a distinguished road
  12. Re: onClick insert into a textbox

    Thanks alot to both of you and i used cal's because its shorter and 100% legal lol.. also it works a treat!! nicely done i put both of your post rep up
    Reply With Quote Reply With Quote
  13. #5
    Join Date
    Oct 2004
    Age
    20
    Posts
    1,967
    Rep Power
    9
  14. Reaper is on a distinguished road
  15. Re: onClick insert into a textbox

    Thanks, I would also use Calamities. It's much cleaner as well, and doesn't have any of those silly vB Functions.
    Reply With Quote Reply With Quote

Similar Threads

  1. Image in Textbox?
    By cancer10 in forum HTML/CSS Coding
    Replies: 15
    Last Post: 04-14-2006, 11:34 PM
  2. Insert into database
    By usmanah in forum PHP Scripting
    Replies: 3
    Last Post: 04-11-2006, 12:20 PM
  3. INSERT x2
    By callumjones in forum PHP Scripting
    Replies: 4
    Last Post: 08-28-2005, 09:47 AM
  4. Paypal, IPN and SQL insert
    By Logan in forum PHP Scripting
    Replies: 9
    Last Post: 08-22-2005, 08:31 AM
  5. Textbox Web Server Control
    By Spy^ in forum General Web Programming
    Replies: 0
    Last Post: 01-02-2005, 09:44 AM