Jump to content

javascript to populate a textarea


technotool

Recommended Posts

Hi any help would be appreciated and rewarded to your donation site.

 

Is there a way to populate a textarea form element with a list of elements (<span>)(onclick)  and then see it populate the textarea in real time.

 

<span id="item1">pizza</span> <span id="item2">coke</span> <span id="item3">marshmello</span> <span id="item4">salad</span> <span id="item5">A1</span> <span id="item6">lettuce</span> <span id="item7">oranges</span> <span id="item8">apples</span>

 

enduser clicks pizza ==> populates textarea in realtime (for visualization)

enduser clicks marshmello ==> populates textarea in realtime (for visualization)

 

<textarea> </textarea>

<submit> ==>sends textarea array to the php processing file for explosion.

 

regards technotool.

 

Link to comment
Share on other sites

This is tried, tested and works in IE7 and Firefox 2. It will add the contents of the span tags to the text area as they are clicked, separating each value with a space. When the submit button is clicked, the page will submit to 'processing.php' - change this to whatever page you want to submit to.

 

HTML:

<div id="span_list"><span id="item1">pizza</span> <span id="item2">coke</span> <span id="item3">marshmallow</span> <span id="item4">salad</span> <span id="item5">A1</span> <span id="item6">lettuce</span> <span id="item7">oranges</span> <span id="item8">apples</span></div>

<form action="processing_page.php" method="post">
   <textarea id="targetTextArea"></textarea>
   <input type="submit" name="submit" value="submit" />
</form>

 

Javascript:

function spanListener()
{
   var spans = document.getElementById("span_list").getElementsByTagName("span")
   var target = document.getElementById("targetTextArea")
   for(var i = 0; i < spans.length; i++)
   {
        spans[i].onclick = function()
        {
             if(target.value != "")
             {
                 target.value = target.value + " "
             }
             target.value = target.value + this.firstChild.data
         }
    }
}

window.onload = function()
{
    spanListener()
}

 

 

Link to comment
Share on other sites

Hi,

 

The below code will populate a <textarea> in realtime(onClick method) with item in the id="span_list". (Web) PageSpace is of real concern and the textarea must be populated with whole paragraphs of information.  If I provide the whole paragraph between the <span id="itemN"></span> --there is too too much text and the page becomes unmanageable. 

 

Is there a way to populate the <textarea>'s with onClick method but each item refers back to a $string or string_array[] which will then populate the textarea for submission to phpprocessing.php file.  Thanks in advance.

 

 

 

<div id="span_list"><span id="item1">index1</span> <span id="item2">index2</span> <span id="item3">index3</span> <span id="item4">index4</span> <span id="item5">index5</span> <span id="item6">index6</span> <span id="item7">index7</span> <span id="item8">index8</span></div>
<form action="processing_page.php" method="post">
   <textarea id="targetTextArea"></textarea>
   <input type="submit" name="submit" value="submit" />
</form>

 

javascript:

function spanListener()
{
   var spans = document.getElementById("span_list").getElementsByTagName("span")
   var target = document.getElementById("targetTextArea")
   for(var i = 0; i < spans.length; i++)
   {
        spans[i].onclick = function()
        {
             if(target.value != "")
             {
                 target.value = target.value + " "
             }
             target.value = target.value + this.firstChild.data
         }
    }
}

window.onload = function()
{
    spanListener()
}

 

 

Link to comment
Share on other sites

So to get this straight, do you mean that you want to have the text in a span refer to something, but you only want to display either part of that something, or a reference to it so as to keep your page uncluttered? And when you click on it, the full version is inserted into the text area?

 

If that's the case it's fairly easy to do, I'll explain it after you confirm.

Link to comment
Share on other sites

Hey Haku,

 

Sorry if I wasn't clear.  What I am trying to achieve is for a list of "reference item_x" "reference item_y" that recall a $variable_x $variable_y (onClick) of the "reference item" to the <textarea></textarea>

 

So:

 

<textarea></textarea>

list:

reference_item_x, reference_item_y

 

onClick event of reference_item_x populates textarea

 

<textarea>$variable_x</textarea>

 

Link to comment
Share on other sites

Hey I am sorry. Thanks for sticking with me on this. I am confusing my php syntax with my javascript syntax.

 

so what I am trying to do is define a variable in the head like this :

<script>
var pizza_pepperoni = "bread, pepperoni, tomato sauce, cheese"
var pizza_cheese= "bread, tomato sauce, cheese"
</script>

and then in the span_list something like this:

<div id="span_list">    
<span id="item1"><script>document.write(pizza_pepperoni)<script></span>                                                             
<span id="item2"><script>document.write(pizza_cheese)<script></span> 
<span id="item3"></span> 

What I get is the variable definition in the span list ie. "Bread, pepperoni, tomato sauce, cheese".  I don't want this. I want the variable to display in the span list and then when it is clicked it populates the textarea with "Bread, pepperoni, tomato sauce, cheese".  If I put qotoes around the arguments in document.write like so:

 

<span id="item1"><script>document.write("pizza_pepperoni")<script></span>                                                             
<span id="item2"><script>document.write("pizza_cheese")<script></span> 

 

It looks right!

 

pizza_pepperoni, pizza_cheese

 

But it populates the textarea with the word "undefined".  I mean I guess this could also be done with <a href /a> tags and += augment loop onto a var variable_x and then have the textarea value equal the var variable_x. but I would like to stick with the current construct. 

 

haku, thanks for your help in advance.

 

 

Link to comment
Share on other sites

Ok, now I gotcha!

 

HTML:

<div id="span_list"><span id="item1"></span> <span id="item2"></span></div>
<form action="processing_page.php" method="post">
   <textarea id="targetTextArea"></textarea>
   <input type="submit" name="submit" value="submit" />
</form>

 

I only did an example with two spans - you will have to add more as necessary.

 

Javascript:

function spanListener()
{
   var spans = document.getElementById("span_list").getElementsByTagName("span")
   var target = document.getElementById("targetTextArea")
   for(var i = 0; i < spans.length; i++)
   {
        spans[i].onclick = function()
        {
		 if(this.firstChild.data == "Pepperoni Pizza")
             {
			 target.value = "bread, pepperoni, tomato sauce, cheese"
             }
             if(this.firstChild.data == "Cheese Pizza")
             {
                 target.value = "bread, tomato sauce, cheese"
             }
         }
    }
}

window.onload = function()
{
    spanListener()
    var target1 = document.getElementById("item1")
    var newText1 = document.createTextNode("Pepperoni Pizza")
    target1.appendChild(newText1)
    var target2 = document.getElementById("item2")
    var newText2 = document.createTextNode("Cheese Pizza")
    target2.appendChild(newText2)    
}

 

That should be close to what you were looking for. You will need to add stuff into the javascript in order to add extra spans. Just copy the pattern that I have given you. I should also mention that if a user has javascript turned off, the spans will be empty and the user won't be able to see anything. On that note, you may want to hard code the span text into the HTML rather than adding it with javascript.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.