...

View Full Version : Insert Text in textarea onclick



defencedog
01-16-2012, 05:08 AM
I have searched this forum for similar queries & have come up with this
http://jsfiddle.net/defencedog/P33FR/

I don't know why this ain't working:mad:

xelawho
01-16-2012, 05:28 AM
works fine for me on the page, but not in the fiddle...



<html>
<head>
<style>
#smly {
background:yellow;
cursor:pointer;
}
</style>
</head>

<body>
<div id="smly">
<img onclick="addText(':)')" src="http://illiweb.com/fa/i/smiles/icon_razz.gif" />
<img onclick="addText(':(')" src="http://illiweb.com/fa/i/smiles/icon_cry.gif" />
</div>

<form>
<textarea id="foo"></textarea>
</form>
<script type="text/javascript">
function addText(smiley) {
document.getElementById('foo').value += " " + smiley + " ";
document.getElementById('foo').focus();

}
</script>
</body>
</html>

Old Pedant
01-16-2012, 05:28 AM
Works fine as a web page. Don't know why it doesn't work in jsfiddle.



<html>
<head>
<script>
function addText(smiley) {
document.getElementById('foo').value += " " + smiley + " ";
document.getElementById('foo').focus();
return false;
}
</script>
<style>
#smly {
background:yellow;
cursor:pointer;
}
</style>
</head>
<body>
<div id="smly">
<img onclick="return addText(':)')" src="http://illiweb.com/fa/i/smiles/icon_razz.gif" />
<img onclick="return addText(':(')" src="http://illiweb.com/fa/i/smiles/icon_cry.gif" />
</div>

You really don't need the returns anywhere in that code. onclick in an image has no use for a return value.

But they obviously don't hurt.

Old Pedant
01-16-2012, 05:29 AM
Hmmm...there seems to be an echo in here...

[But I guess we learned to not trust jsfiddle, didn't we?]

defencedog
01-16-2012, 06:00 AM
Thnx for all the answers

You really don't need the returns anywhere in that code. onclick in an image has no use for a return value.
hey sir isn't return a strictly valid syntax to send a value to a function specified elsewhere. Plus I have seen here people have used 'Javascript:addText()' what is the bold part for

Old Pedant
01-16-2012, 06:26 AM
isn't return a strictly valid syntax to send a value to a function specified elsewhere.

No. return is use to return a value *FROM* the *CURRENT FUNCTION*.

It's always valid to return a value from any function. It's just sometimes not needed.



<img ... onclick="return xxxx();" />

It is *NOT* needed in an <img> tag.


<a ... onclick="return xxxx();">

It likely *is* needed in an <a> tag, because returning false from the onclick of an <a> tag suppresses the normal action of the <a> tag (which is to jump to the specified href location).

******


Plus I have seen here people have used 'Javascript:addText()' what is the [red] part for

The only time that is really needed is if you are on a page in MSIE where you have both JavaScript and VBScript functions being used to handle events and you want to make sure that the browser knows that this event handler will be coded as JavaScript. Otherwise, it's kind of a waste of code.

Well, there's one other place you might use it:

<a href="javascript:doSomething();">
that basically tells the browser that the href here is *NOT* specifying a URL. Personally, I don't like to use that. I much prefer to use something like

<a href="#" onclick="doSomething(); return false;">

But each to his/her own.

defencedog
01-17-2012, 06:49 AM
Fiddle bug solved

In the sidebar, there's a drop down. Select "no wrap (head)" and your script will work.
http://i42.servimg.com/u/f42/16/42/09/78/der10.png



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum