I am making a little forum and while trying to add the quote post functionality have come across a problem that has me stumped. Here is my js:

function addQuote(text)
	{
		var tArea = document.getElementById('forumReply_message');
		tArea.value = text;
	
	}

Here is sample HTML (one that doesn't work):

<a href="#replyArea" onclick="addQuote('Got a random playlist on right now.  All sorts of stuff... Miike Snow, Galactic, Danger Mouse, Bon Iver, Marlena Shaw, Dirty Projectors... it's eclectic to say the least.');">Quote</a>

I really can't figure out why this won't pass the addQuote() value into the textarea. It does work for some posts but most of them it doesn't. I appreciate any help.

Dani AI

Generated

Good troubleshooting so far — correctly pinned the immediate cause (an unescaped apostrophe inside an inline JavaScript string) and was off the mark about innerHTML. The DOM value property is the right way to change a textarea at runtime; the failure here came from how the quoted text was embedded in the HTML/javascript, not from value itself. See the spec for the textarea interface for details: HTMLTextAreaElement.value.

Safer patterns to avoid brittle inline handlers and escaping problems:

  • Put the post text into a data attribute or a hidden element, and bind a click handler with addEventListener. That keeps escaping limited to HTML attribute rules and avoids building JS code strings in markup.
  • If an inline JS literal must be generated server-side, produce a valid JS string literal (for PHP, json_encode($text) is a robust choice) so quotes, backslashes and newlines are escaped correctly (json_encode).

Example workflows (conceptual):

  • Server emits <button data-quote="...">Quote</button> and client JS reads el.dataset.quote then assigns that text to the textarea value.
  • Or server emits onclick="useQuote(<?php echo json_encode($postText); ?>)" so the JS string is safely escaped.

Troubleshooting checklist when a quote fails to appear:

  • Open DevTools console — a broken inline string will usually throw a SyntaxError at parse time.
  • Inspect the rendered HTML/attribute value (View Source or Elements panel) to confirm whether quotes were escaped or left raw.
  • Check server-side escaping chains (htmlspecialchars, addslashes, magic quotes) — mixing HTML escaping and JS string requirements is a common trap.

For reading/writing non-HTML text (including newlines), rely on the textarea value and avoid inserting markup via innerHTML. For attribute-driven approaches, see dataset docs: HTMLElement.dataset.

Recommended Answers

All 4 Replies

If tArea is a <textarea> then .value won't work, you'll have to user innerHTML

I am making a little forum and while trying to add the quote post functionality have come across a problem that has me stumped. Here is my js:

function addQuote(text)
	{
		var tArea = document.getElementById('forumReply_message');
		tArea.value = text;
	
	}

Here is sample HTML (one that doesn't work):

<a href="#replyArea" onclick="addQuote('Got a random playlist on right now.  All sorts of stuff... Miike Snow, Galactic, Danger Mouse, Bon Iver, Marlena Shaw, Dirty Projectors... it's eclectic to say the least.');">Quote</a>

I really can't figure out why this won't pass the addQuote() value into the textarea. It does work for some posts but most of them it doesn't. I appreciate any help.

Are you sure? For one, why does it work some of the time? Secondly why does every single tutorial on the subject I have come across say that .value is the way to do this?

I will try out the .innerhtml and see how it works, but I am curious why .value works some of the time and not others.

You made a quote mistake:

<a href="#replyArea" onclick="addQuote('Got a random playlist on right now.  All sorts of stuff... Miike Snow, Galactic, Danger Mouse, Bon Iver, Marlena Shaw, Dirty Projectors... [B]it's [/B]eclectic to say the least.');">Quote</a>

Needs to be:

<a href="#replyArea" onclick="addQuote('Got a random playlist on right now.  All sorts of stuff... Miike Snow, Galactic, Danger Mouse, Bon Iver, Marlena Shaw, Dirty Projectors... it\'s eclectic to say the least.');">Quote</a>

With textarea's, the .value property does work. In fact, if the text contains \n's, and you use innerHTML to add the text into the textarea, it results into a weird error.

~G

Ah ha! I thought I ran that through PHP's htmlspecialchars with ENT_QUOTES on, looks like I'll have to look in to that a little more. Thanks for the help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.