Go Back To List
Forms (All Types)
Insert text from 2 inputs into a TextArea a cursor position
Next Article
Textarea Character Count (Memo Field Character Count)
Coding Article #: 9 - Published On: September 02, 2010 @ 22:55:09 PM - Last Updated on: June 20, 2012
This article has been Favorited 6 times
Join today, and add this to your favorites.
Coding Source - Share on MySpace Coding Source - Share With Facebook Coding Source - Share on Twitter Coding Source - Share on Reddit Coding Source - Share on Digg It Coding Source - Share on Stumble Upon It Coding Source - Share on Delicious
Share With Friends (Updated 6-8-2010)

Supported Files
No Files for this Article.
No Screenshot Available
This code will allow for the user to create a link with name and URL
Without having to write out the BBcode.

(Instructions)
What we have to do is this:

function insertAtCursor(myField, myValue)

The function is only looking at 1 myValue, so it is only getting the 1st value that is sent to it.
Now, in order to use the other values, we need to add another myValue to the function

function insertAtCursor(myField, myValue, myValue1)

Now, we can use the myValue1 in the same area's of the code as we were using the myValue by itself.

First, we need our Javascript function insertAtCursor.
Place this in the head of your page.

<script type="text/javascript">
    function insertAtCursor(myField, myValue, myValue1) {
        //IE support
        if (document.selection) {
            myField.focus();
            sel = document.selection.createRange();
            sel.text = "[url=" + myValue + "]";
            sel.text = "" + myValue1 + "[/url]";
        }
        //MOZILLA/NETSCAPE support
        else if (myField.selectionStart || myField.selectionStart == '0') {
            var startPos = myField.selectionStart;
            var endPos = myField.selectionEnd;
            myField.value = myField.value.substring(0, startPos) +
                "[url=" + myValue + "]" +
                "" + myValue1 + "[/url]" +
                myField.value.substring(endPos, myField.value.length);
        } else {
            myField.value += "[url=" + myValue + "]";
            myField.value += "" + myValue1 + "[/url]";
        }
    }
</script>


Next, we create our form.
With a Textarea, 2 fields, and a button
With the OnClick, we will use it to insert the text fields values, into the textarea.

<form name = "myform">
<label>Content</label>
<div><textarea name = "txta" rows = "10" cols = "50"></textarea></div>
<label>URL</label>
<div><input type="text" name="txtb" value="" /></div>
<label>Name</label>
<div><input type="text" name="txtc" value="" /></div>
<input type="button" value="Add Text" onclick="insertAtCursor(document.myform.txta, document.myform.txtb.value, document.myform.txtc.value)">
</form>

Post to Facebook about: Insert text from 2 inputs into a TextArea a cursor position