• 0

[JavaScript] Appending text to an HTML Textbox


Question

I'm looking to have append text to an HTML Textbox much like when creating a new thread here you can click IMG and link to an image or use the http:// button to add a link.

I can get JavaScript to place text in a textbox but it replaces whatever is already in there. Can anybody help me with that? Thanks!

6 answers to this question

Recommended Posts

  • 0

There you go. Took me 2 minutes. :)

<html>
<head>
	<script language="javascript">
	<!--
  function insertText()
  {	
 	 var url
 	 url=prompt("Enter URL below:", "http://");
 	 document.frm.box.value="[URL]"+url+"[/URL]";
  }
	//-->
	</script>
</head>
<body>
	<form name="frm">
  <input type="text" name="box" />
  <input type="button" name="insertTxt" onClick="insertText();" value="Insert Text"/>
	</form>
</body>
</html>

  • 0
There you go. Took me 2 minutes. :)

<html>
<head>
	<script language="javascript">
	<!--
 ?function insertText()
 ?{	
 ?	var url
 ?	url=prompt("Enter URL below:", "http://");
 ?	document.frm.box.value="[URL]"+url+"[/URL]";
 ?}
	//-->
	</script>
</head>
<body>
	<form name="frm">
 ?<input type="text" name="box" />
 ?<input type="button" name="insertTxt" onClick="insertText();" value="Insert Text"/>
	</form>
</body>
</html>

Well, what if you already have text in the box? That will replace everything. You can do "+=" to put it on the end, but I need to be able to put it in the middle of the text box with text above and below where it inserts it.

  • 0

You can do it using some caret thinggy. You can see it at http://www.faqts.com/knowledge_base/view.p...id/1052/fid/130.

I have an example here on how to use it.

<html>
<head>
<title>Some page</title>
<script type="text/javascript">
// Insert at Claret position. Code from
// http://www.faqts.com/knowledge_base/view.phtml/aid/1052/fid/130
current = "nothing"
function storeCaret(textEl) {
	if (textEl.createTextRange) textEl.caretPos = document.selection.createRange().duplicate();
current = textEl
}
function addcode(text,whatever){
if (whatever == "nothing"){
alert("Error, please select a field to insert the smile/code.")
}
else if (whatever.createTextRange && whatever.caretPos) {
  var caretPos = whatever.caretPos;
  caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text + ' ' : text;
  whatever.focus();
	} else {
	whatever.value  += text;
	whatever.focus();
	}
}
</script>
</head>
<body>
<input type="text" name="comment" size="8" onselect="storeCaret(this);" onclick="storeCaret(this);" onkeyup="storeCaret(this);">
<br><br><a href='javascript:addcode("text",current)'>Add text</a>
</body>
</html>

In this example, when u click on the text field and u click the add text link, it will add the text there Try it. You can examine the code for more info.

P.S.: I use this on ym site, go there then go to the tagboard and try it.

This topic is now closed to further replies.
  • Posts

    • Brave Browser 1.91.171 by Razvan Serea Brave Browser is a lightning-fast, secure web browser that stands out from the competition with its focus on privacy, security, and speed. With features like HTTPS Everywhere and built-in tracker blocking, Brave keeps your online activities safe from prying eyes. Brave is one of the safest browsers on the market today. It blocks third-party data storage. It protects from browser fingerprinting. And it does all this by default. Speed - Brave is built on Chromium, the same technology that powers Google Chrome, and is optimized for speed, providing a fast and responsive browsing experience. Brave Browser also features Brave Rewards, a system that rewards users with Basic Attention Tokens (BAT) for viewing opt-in ads. This innovative system provides an alternative revenue model for content creators and a way to support the Brave community. SlimBrave Neo takes all the good things about Brave and makes them even better by keeping everything clean, light, and privacy-focused. It removes the extra clutter, turns off features you might not need, and cuts down on anything that could slow you down or collect unnecessary data. Because it relies on simple settings and policies instead of modifying the browser itself, you still get full Brave compatibility—just in a smoother, lighter, and more privacy-friendly package. Brave Browser 1.91.171 changelog: General Fixed Cardano not being disabled on upgrade to Brave Origin. Upgraded Chromium to 149.0.7827.103. Origin Removed “Survey Panelist” setting from brave://settings/privacy. Fixed P3A and usage ping under brave://settings/privacy being displayed on first launch on Linux. Upgraded Chromium to 149.0.7827.103. Download: Brave Browser 64-bit | 1.2 MB (Freeware) Download: Brave Browser 32-bit View: Brave Homepage | Offline Installers | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • Hi. As the title suggests, I can't access the forum on my phone. I'm using Edge on Android and when I try to navigate to the forum I get a "we value your privacy" popup and none of the buttons are clickable. It effectively stonewalls me from reading any forum content.
    • Honestly you're not wrong about AdGuard. Neowin frequently has lifetime license discounts for them and that's how I got my cheap family license a few years ago to run it on all my devices.
  • Recent Achievements

    • One Year In
      slackerzz earned a badge
      One Year In
    • One Year In
      highriskpaym earned a badge
      One Year In
    • One Month Later
      highriskpaym earned a badge
      One Month Later
    • Week One Done
      highriskpaym earned a badge
      Week One Done
    • Week One Done
      FBSPL earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      521
    2. 2
      PsYcHoKiLLa
      197
    3. 3
      +Edouard
      157
    4. 4
      Steven P.
      84
    5. 5
      ATLien_0
      75
  • Tell a friend

    Love Neowin? Tell a friend!