Jump to content

Duplication


supanoob

Recommended Posts

ok so i have a piece of code that is used to insert text from a button when clicked, into a textarea, it all works well apart from the fact that whenever i click a second button to insert something into the field, it is added with whatever was clicked previously for example, i click a :) button, and then i click :( it would be added like so :):(:).

 

Here is the javascript code:

var globalCursorPos; 

function setCursorPos() {
globalCursorPos = getCursorPos(postForm.post_body);
}


function getCursorPos(textElement) {

var sOldText = textElement.value;

var objRange = document.selection.createRange();
var sOldRange = objRange.text;

var sWeirdString = '#%~';

objRange.text = sOldRange + sWeirdString; objRange.moveStart('character', (0 - sOldRange.length - sWeirdString.length));

var sNewText = textElement.value;

objRange.text = sOldRange;

for (i=0; i <= sNewText.length; i++) {
   var sTemp = sNewText.substring(i, i + sWeirdString.length);
   if (sTemp == sWeirdString) {
     var cursorPos = (i - sOldRange.length);
     return cursorPos;
   }
}
}

function insertString(stringToInsert) {
var firstPart = postForm.post_body.value.substring(0, globalCursorPos);
var secondPart = postForm.post_body.value.substring(globalCursorPos, postForm.post_body.value.length);
postForm.post_body.value = firstPart + stringToInsert + secondPart;
}

 

and here is the html with the text field:

<form id="form1" name="postForm" method="post" action="">
<table width="100%" border="0" class="post_center">
  <tr>
    <td><center>
  <table width="95%" border="0">
    <tr>
      <td colspan="2" class="post">
                      <input TYPE="button" VALUE="BlahBlah" ONCLICK="insertString(this.value)" class="button">
                      <input TYPE="button" VALUE="YeaYea" ONCLICK="insertString(this.value)" class="button">
                      <input TYPE="button" VALUE="YupYup" ONCLICK="insertString(this.value)" class="button"></td>
    </tr>
    <tr>
      <td class="post" width="25%">Topic</td>
      <td class="post"><label>
                      <input type="text" name="textfield" id="textfield" />
      </label></td>
    </tr>
    <tr>
      <td class="post">Body</td>
      <td class="post"><label>
                      <textarea name="post_body" id="post_body" cols="45" rows="5"></textarea>
      </label></td>
    </tr>
    <tr>
      <td colspan="2" class="post"><label>
        <div align="center">
                      <input type="submit" name="button" id="button" value="Post" />
          </div>
      </label></td>
    </tr>
  </table>
  </CENTER>
</td>
  </tr>
</table>
</form>

 

Any help would be much appreciated.

 

Link to comment
https://forums.phpfreaks.com/topic/209596-duplication/
Share on other sites

why is the insertString function grabbing first and second parts. If you just want to append to the text area value the first and second parts are not necessary. Below I am doing a check to see if a value exists then insert a space before adding the new value to the text area.

 

function insertString(stringToInsert) {

 

if (postForm.post_body.value.length > 0) { 

            postForm.post_body.value += '  ' + stringToInsert;

        } else {

            postForm.post_body.value = stringToInsert;

}

 

}

Link to comment
https://forums.phpfreaks.com/topic/209596-duplication/#findComment-1094806
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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