www.webdeveloper.com
Results 1 to 2 of 2

Thread: simple forum code to HTML javascript

  1. #1
    Join Date
    Jun 2008
    Posts
    2

    simple forum code to HTML javascript

    I need a basic version of a form's text to html script. I have it working in firefox, but somereason it doesn't work in IE7. In IE7 it pastes the text, but at the end of the textarea, instead of around my currently selected text. ( firefox3 the code works fine )

    What's wrong?

    code for the button that's clicked
    Code:
    <a onclick="insertTags('<b>', '</b>', document.forms.formpost.text);return false;" href="javascript:void(0);">[ Bold ]</a>
    and the .js
    Code:
    // Surrounds the selected text with text1 and text2.
    function insertTags(text1, text2, textarea)
    {
    	// Can a text range be created?
    	if (typeof(textarea.caretPos) != "undefined" && textarea.createTextRange)
    	{
    		var caretPos = textarea.caretPos, temp_length = caretPos.text.length;
    
    		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text1 + caretPos.text + text2 + ' ' : text1 + caretPos.text + text2;
    
    		if (temp_length == 0)
    		{
    			caretPos.moveStart("character", -text2.length);
    			caretPos.moveEnd("character", -text2.length);
    			caretPos.select();
    		}
    		else
    			textarea.focus(caretPos);
    	}
    	// Mozilla text range wrap.
    	else if (typeof(textarea.selectionStart) != "undefined")
    	{
    		var begin = textarea.value.substr(0, textarea.selectionStart);
    		var selection = textarea.value.substr(textarea.selectionStart, textarea.selectionEnd - textarea.selectionStart);
    		var end = textarea.value.substr(textarea.selectionEnd);
    		var newCursorPos = textarea.selectionStart;
    		var scrollPos = textarea.scrollTop;
    
    		textarea.value = begin + text1 + selection + text2 + end;
    
    		if (textarea.setSelectionRange)
    		{
    			if (selection.length == 0)
    				textarea.setSelectionRange(newCursorPos + text1.length, newCursorPos + text1.length);
    			else
    				textarea.setSelectionRange(newCursorPos, newCursorPos + text1.length + selection.length + text2.length);
    			textarea.focus();
    		}
    		textarea.scrollTop = scrollPos;
    	}
    	// Just put them on the end, then.
    	else
    	{
    		textarea.value += text1 + text2;
    		textarea.focus(textarea.value.length - 1);
    	}
    }
    thanks,

  2. #2
    Join Date
    Jun 2008
    Posts
    2
    If you don't see anything wrong with the IE code, do you know a website that has a javascript example that does this for IE?

    All I could find, ( googling alot ) is insert text at cursor. But I need to insert text around selection.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles