Putting a user input in a textarea onload

D

danielboendergaard

Hey
Im making a homepage in php. I use a html form to put data into mysql
and i want to make some buttons which inserts user input values into a
textarea. I have used a button like this:

<input type="button" value="Add Quote"
onclick="document.getElementById('nyhed').value+=''">

The button works fine and insterts into the textarea.
Although I want to make a button which onclick asks the user for a
input and inserts it into the textarea. The script would in this case
ask for an input and insert
USER INPUT
into the
textarea.

Does someone know how to do that?
 
R

RobG

(e-mail address removed) said on 24/04/2006 5:06 PM AEST:
Hey
Im making a homepage in php. I use a html form to put data into mysql
and i want to make some buttons which inserts user input values into a
textarea. I have used a button like this:

<input type="button" value="Add Quote"
onclick="document.getElementById('nyhed').value+=''">

The button works fine and insterts into the textarea.
Although I want to make a button which onclick asks the user for a
input and inserts it into the textarea. The script would in this case
ask for an input and insert
USER INPUT
into the
textarea.

Why not let the user enter whatever into the textarea and add the quote
tags when you get the response at the server?

You can use a prompt to get user input, but it's rather kludgy to use a
dialog to get input that is dropped into a page anyway (I hate such things).

<textarea onclick="
this.value = '
'
+ prompt('Enter some text')
+ '
';
"></textarea>


But no recommended.
 
T

Thomas 'PointedEars' Lahn

RobG said:
(e-mail address removed) said on 24/04/2006 5:06 PM AEST:
Im making a homepage in php. I use a html form to put data into mysql
and i want to make some buttons which inserts user input values into a ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
textarea. I have used a button like this:

<input type="button" value="Add Quote"
onclick="document.getElementById('nyhed').value+=''"> ^^^^^^^^^^^^^^^
The button works fine and insterts into the textarea.
Although I want to make a button which onclick asks the user for a
input and inserts it into the textarea. The script would in this case
ask for an input and insert
USER INPUT
into the
textarea.

Why not let the user enter whatever into the textarea and add the quote
tags when you get the response at the server?

I think you miss the point :)

Obviously, this is for some kind of bulletin board or Wiki, where
the "
" markup is to indicate text quoted from the
precursor or a third source. (Have you never used such before? ;-))
You can use a prompt to get user input, but it's rather kludgy to use a
dialog to get input that is dropped into a page anyway (I hate such
things).

<textarea onclick="
this.value = '
'
+ prompt('Enter some text')
+ '
';
"></textarea>

Since prompt() returns `false' if the user cancels, one wants to enhance
this a bit at least (watch for word-wrap):

<textarea
onclick="var s; this.value = '
' + ((s = prompt('Enter some
text')) ? s : "") + '
';"
</textarea>
But no recommended.

True. Newline in attribute values is not recommended. And one should use
a button instead, as the OP already did (only his referencing was
unnecessarily complicated and error-prone). That way the button could be
generated by script (so that without script, users do not have to worry
about a dysfunctional button) and could facilitate for inserting the markup
at the cursor position, or replace the current selection. Quick hack for
Geckos:

<meta http-equiv="Content-Script-Type" content="text/javascript">
...
<form ...>
...
<script type="text/javascript">
function addQuote(o)
{
// if nothing is selected, selStart == selEnd
var
selStart = o.selectionStart,
selEnd = o.selectionEnd,
s;

if ((s = window.prompt('Enter some text'))
{
oSel.value = oSel.value.substring(0, selStart)
+ '
' + s + '
'
+ oSel.value.substr(selEnd);
}
}

document.write('<input type="button"'
+ ' value="Add Quote"'
+ ' onclick="addQuote(this.form.elements[\'nyhed\']);">');
</script>

<textarea name="nyhed" ...></textarea>

...
</form>

See <URL:http://faqts.com/knowledge_base/view.phtml/aid/17749/fid/53> (or
Wikimedia etc. source code) for (a hint towards) a cross-browser approach.


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top