• 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.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • When will the Photos app be updated to remember the window size and position when reopened? They addressed this issue in a 2024 version of the app (though I can't recall the build number). Unfortunately, after that specific version, the problem persists! Please prioritise this fix in your K2 schedule. Additionally, the Snipping Tool has lost the ability to capture the Windows Taskbar starting from the 2024 version!
    • Same, never saw it on Android or iOS. Guess only some people got it *shrugs*
    • Anthropic pulls Fable 5 and Mythos 5 after US export control order by Pradeep Viswanathan In April this year, Anthropic launched the Claude Mythos Preview frontier model with state-of-the-art cyber and coding capabilities for a select set of companies around the world. After preparing appropriate guardrails, early this week, Anthropic launched Claude Fable 5 and Mythos 5, its most capable AI models. Claude Fable 5 is for general users and comes with strict safeguards, while Mythos 5 is designed with fewer safeguards for cybersecurity and biology use cases. Today, Anthropic abruptly suspended access to its Fable 5 and Mythos 5 AI models for all customers after receiving an export control directive from the US government. The company received the directive from the government today at 5:21 p.m. ET, and the received letter did not provide any details regarding the national security concern. Anthropic understands that the government became aware of a method to bypass, or “jailbreak,” Fable 5, which might be the reason behind the directive. The order was issued under national security authorities and requires the company to suspend all access to Fable 5 and Mythos 5 by any foreign national, whether they are inside or outside the United States. The restriction also applies to foreign national employees working at Anthropic. As a result, the company has disabled both models for all customers to ensure compliance. Access to previous Anthropic models like Opus and Sonnet is not affected by this government order. The company highlighted that it had developed strong safeguards to reduce the possibility that Fable is misused for tasks related to cybersecurity. In fact, many developers are complaining that the safeguards are going overboard. Additionally, the company worked with the US government, the UK AISI, multiple private third-party organizations, and internal teams to red-team Fable’s safeguards for thousands of hours. Finally, Anthropic noted that no testers have yet been able to find a universal jailbreak on Fable 5. As expected, Anthropic disagrees that a narrow potential jailbreak should lead to the recall of a commercial model used by hundreds of millions of people. It warned that applying this standard across the AI industry could effectively halt new frontier model deployments. Anthropic concluded by mentioning that it is working to restore access to Fable 5 and Mythos 5 as soon as possible and plans to share more details within the next 24 hours.
    • Brave Browser 1.91.172 is out.
  • Recent Achievements

    • Contributor
      MarkHughes4096 went up a rank
      Contributor
    • Dedicated
      jordanspringer earned a badge
      Dedicated
    • Rookie
      Rimplesnort went up a rank
      Rookie
    • One Year In
      Markus94287 earned a badge
      One Year In
    • One Month Later
      Markus94287 earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      503
    2. 2
      +Edouard
      176
    3. 3
      PsYcHoKiLLa
      147
    4. 4
      ATLien_0
      92
    5. 5
      Steven P.
      79
  • Tell a friend

    Love Neowin? Tell a friend!