Hi
I am trying to use onclick to add text to a textarea, the function I am using is:

function addtxt(input,add) {
var obj=document.getElementById(input)
var txt=document.createTextNode(add)
obj.appendChild(txt)
}

and the line that I use to call the function - through php- is:

<a onclick=\"addtxt('post','$code');\">
                       <img src=\"smiles/$smile\" alt=\"$smilename\" border=\"0\" style=\"cursor: pointer;\"></a>

it seems to work in IE but not in firefox, why is that?

Recommended Answers

All 3 Replies

Try this demo:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<html id="html40L" lang="en">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<meta http-equiv="Window-target" content="_top">
<title>Free Live Help!</title>
</head>
<body>
<div id="main">
<form id="form" name="form" action="#" onsubmit="return false;">
<div>
<fieldset><legend>Test Field</legend><br><br>
<input type="text" id="text" name="text" value=""><br><br>
<textarea id="txt" name="txt" cols="50" rows="5"></textarea><br><br>

<a href="javascript:void(0);" onclick="form.txt.value = 'Hello World!'">&lt;textarea&gt; Add text</a><br><br>
<a href="javascript:void(0);" onclick="form.text.value = 'Hello Universe!'">&lt;input type="text"&gt; Add text</a>
</fieldset>
</div>
</form>
</div>
</body>
</html>

khr2003,

That code certainly looks like it should work but clearly FF doesn't like adding text that way!

Try this:

function addtxt(input,add) {
var obj=document.getElementById(input);
obj.innerHTML += add;
}

If you want a line break then obj.innerHTML += "\n" + add; .

(Currently away from home so no testing I'm afraid)

Airshow

thank you very much

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.