Reply
A question
Old 03-21-2005, 05:52 PM A question
dan245's Avatar
Skilled Talker

Posts: 59
Location: Massachusetts, USA
Is it possible to write text to a textarea from document.write()?
dan245 is offline
Reply With Quote
View Public Profile
 
When You Register, These Ads Go Away!
     
Old 03-21-2005, 06:35 PM
Christopher's Avatar
Iced Cap

Latest Blog Post:
Unicode and Character Sets
Posts: 3,108
Location: Toronto, Ontario
No, you just alter the value of the textarea (see here for some more info on the textarea object).

For example:
HTML Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Test</title>
    <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
    <script type="text/javascript">
        function fillTextarea(num)
        {
            if(!num || num < 1 || num > 4)
                num = 1;

            textarea = eval('document.myform.textarea' + num);

            switch(num)
            {
                case 1:
                    textarea.value = "This";
                    break;
                
                case 2:
                    textarea.value = "is";
                    break;
                
                case 3:
                    textarea.value = "a";
                    break;
                
                case 4:
                    textarea.value = "test.";
                    break;
            }
        }
    </script>
</head>
<body>

<form name="myform">

<textarea name="textarea1"></textarea><br />
<input type="button" onclick="fillTextarea(1)" value="Fill" />

<br /><br />

<textarea name="textarea2"></textarea><br />
<input type="button" onclick="fillTextarea(2)" value="Fill" />

<br /><br />

<textarea name="textarea3"></textarea><br />
<input type="button" onclick="fillTextarea(3)" value="Fill" />

<br /><br />

<textarea name="textarea4"></textarea><br />
<input type="button" onclick="fillTextarea(4)" value="Fill" />

</form>

</body>
</html>
Christopher is offline
Reply With Quote
View Public Profile Visit Christopher's homepage!
 
Old 03-21-2005, 07:47 PM
dan245's Avatar
Skilled Talker

Posts: 59
Location: Massachusetts, USA
cool, thank you chroder!
dan245 is offline
Reply With Quote
View Public Profile
 
Old 03-21-2005, 07:52 PM
dan245's Avatar
Skilled Talker

Posts: 59
Location: Massachusetts, USA
Oh, I also forgot to ask, I can only add 1 word in each textarea, is there a way to add the same word by clicking the same button?
dan245 is offline
Reply With Quote
View Public Profile
 
Old 03-21-2005, 08:23 PM
Christopher's Avatar
Iced Cap

Latest Blog Post:
Unicode and Character Sets
Posts: 3,108
Location: Toronto, Ontario
Like this?

HTML Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Test</title>
    <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
    <script type="text/javascript">
        function fillTextarea(txt, name, min, max)
        {
            for(var i = min; i <= max; i++)
            {
                textarea = eval("document.myform." + name + i);

                if(!textarea)
                    continue;
                
                textarea.value = txt;
            }
        }
    </script>
</head>
<body>

<form name="myform">

<textarea name="textarea1"></textarea><br />
<textarea name="textarea2"></textarea><br />
<textarea name="textarea3"></textarea><br />
<textarea name="textarea4"></textarea><br />
<textarea name="textarea5"></textarea><br />
<textarea name="textarea6"></textarea><br />
<textarea name="textarea7"></textarea><br />
<textarea name="textarea8"></textarea><br />

<input type="button" onclick="fillTextarea('This is my string.', 'textarea', 1, 8)" value="Fill All" />

</form>

</body>
</html>
This is just a modified version, taking four arugments
  • txt - The string you want to fill the textareas with.
  • name - The textarea names without the number (ie. if you had "description1" to "description8", the name without the number would be simply "description")
  • min - The minimum number suffix added to the name (in the above example, 1)
  • max - The maximum number suffix added to the name (in the above example, 8)
Christopher is offline
Reply With Quote
View Public Profile Visit Christopher's homepage!
 
Old 03-21-2005, 09:04 PM
dk01's Avatar
Ultra Talker

Posts: 373
Location: Ames, IA
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Test</title>
    <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
    <script type="text/javascript">
        function fillTextarea(textareaId, txt)
        {
            document.getElementById(textareaId).value = document.getElementById(textareaId).value + txt;
        }
    </script>
</head>
<body>

<form name="myform">
<textarea id="textarea1"></textarea><br />
<input type="button" onclick="fillTextarea('textarea1','Repeat this. ')" value="Fill" />
<input type="button" onclick="fillTextarea('textarea1','Blah. ')" value="Fill" />
</form>

</body>
</html>
This is what you are looking for. You can have different buttons for different words too.
__________________
Did I help you? If so, be nice and throw me some TP
dk01 is offline
Reply With Quote
View Public Profile Visit dk01's homepage!
 
Old 03-22-2005, 06:57 AM
Average Talker

Posts: 15
Hey, I was just looking for such solution for my site. Thanks.
bunker is offline
Reply With Quote
View Public Profile
 
Old 03-22-2005, 01:11 PM
dan245's Avatar
Skilled Talker

Posts: 59
Location: Massachusetts, USA
Cool, thank you soo much!!
dan245 is offline
Reply With Quote
View Public Profile
 
Old 03-22-2005, 04:11 PM
dan245's Avatar
Skilled Talker

Posts: 59
Location: Massachusetts, USA
another question again!

I want to make a link be displayed when I click the button...

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    
<title>Test</title>
    
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
    
<script type="text/javascript">

function hyperLink(textareaId, txt)

{

var linkname = prompt ( "Please Specify the name of your link..." );

var link = prompt ( "Please specify the location of the link..." );

document.getElementById(textareaId).value = document.getElementById(textareaId).value + txt;        

}
    
</script>

</head>
<body>

<form name="myform">

<input type="button" value="Create Hyperlink" onclick="hyperLink('myeditor','<a href=link>linkname</a>')">

<br />

<textarea id="myeditor" cols="25" rows="15"></textarea>

</form>

</body>
</html>
When I click the button, it comes out with <a href=link>linkname</a>, but I want it to be the values set in the prompt.
dan245 is offline
Reply With Quote
View Public Profile
 
Old 03-22-2005, 04:41 PM
Christopher's Avatar
Iced Cap

Latest Blog Post:
Unicode and Character Sets
Posts: 3,108
Location: Toronto, Ontario
HTML Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Test</title>
    <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
    <script type="text/javascript">
        function getObj(id)
        {
            var obj = false;

            if(document.getElementById)
                obj = document.getElementById(id);
            else if(document.all)
                obj = document.all[id];
            else if(document.layers)
                obj = document.layers[id];

            return obj;
        }

        function hyperLink(textareaId)
        {
            textarea = getObj(textareaId);

            if(!textarea)
                return;

            var linkname = prompt ( "Please Specify the name of your link..." );
            var link = prompt ( "Please specify the location of the link..." );
            
            textarea.value += '<a href="' + link + '">' + linkname + '</a>';
        }
    </script>
</head>
<body>

<form name="myform">

<input type="button" value="Create Hyperlink" onclick="hyperLink('myeditor')">

<br />

<textarea id="myeditor" cols="25" rows="15"></textarea>

</form>

</body>
</html>
(I've included another function, getObj. You can use this and it will work with different DOM's. That is to say, old IE's and Netscapes, or those that don't support the W3C DOM.)

Anyway, instead of specifying the text as an argument, just create the text within the function.
Christopher is offline
Reply With Quote
View Public Profile Visit Christopher's homepage!
 
Old 03-23-2005, 01:07 AM
dk01's Avatar
Ultra Talker

Posts: 373
Location: Ames, IA
edit:// I see someone already got to this.

-dk
__________________
Did I help you? If so, be nice and throw me some TP
dk01 is offline
Reply With Quote
View Public Profile Visit dk01's homepage!
 
Reply     « Reply to A question
 

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.17563 seconds with 13 queries