...

View Full Version : Insert Text Into One of Multiple Textareas



roxii
04-19-2005, 08:07 PM
Ok, I've given up on the insert text at carat idea, it seems to be impossible cross browser. I decided I will focus on getting help for this problem then.

How do I get a link to insert text in the textbox that was last focused upon?


<form name="form1" action="">
<textarea name="textarea1" onfocus="focus(this)"></textarea>
<textarea name="textarea2" onfocus="focus(this)"></textarea>
<a href="javascript:insert('<img src=&quot;picture1.gif&quot; alt=&quot;picture1.gif&quot;>')">picture1.gif</a>
</form>


I'm no great shakes at writing javascript. Everything I've tried for this fails, but what I think it needs would be a simple function 'focus' that knows what textarea was last focused on. Then when you click on the link 'picture1.gif', it runs the second function 'insert' that sticks '<img src="picture1.gif" alt="picture1.gif">' into the last focused upon textarea.

That sound right? Would anyone be so kind as to help me out with the javascript portion of it?

Mr J
04-19-2005, 09:45 PM
Do you mean something like this



<script type="text/javascript">
<!--
function chk(n){
v=n
}

function insert(str){
if(v==1){
document.forms["form1"]["textarea1"].value=str
}

if(v==2){
document.forms["form1"]["textarea2"].value=str
}

}
//-->
</script>


<form name="form1" action="">
<textarea name="textarea1" onclick="chk(1)"></textarea>
<textarea name="textarea2" onclick="chk(2)"></textarea>
<a href="#null" onclick="insert('<img src=&quot;picture1.gif&quot; alt=&quot;picture1.gif&quot;>')">picture1.gif</a>
</form>

roxii
04-19-2005, 10:46 PM
Do you mean something like this



<script type="text/javascript">
<!--
function chk(n){
v=n
}

function insert(str){
if(v==1){
document.forms["form1"]["textarea1"].value=str
}

if(v==2){
document.forms["form1"]["textarea2"].value=str
}

}
//-->
</script>


<form name="form1" action="">
<textarea name="textarea1" onclick="chk(1)"></textarea>
<textarea name="textarea2" onclick="chk(2)"></textarea>
<a href="#null" onclick="insert('<img src=&quot;picture1.gif&quot; alt=&quot;picture1.gif&quot;>')">picture1.gif</a>
</form>


Well, that's halfway there. I want it to add the contents of the insert statement rather than just deleting everything in the textarea and adding it.

That seems to be the only thing. When I tested it, I added several links with different statements to insert and that worked just fine.

Mr J
04-20-2005, 01:56 PM
How about this.



<script type="text/javascript">
<!--
v=0
function chk(n){
v=n
}

function insert(str){
if(v==0){
return
}
if(v==1){
document.forms["form1"]["textarea1"].value+=str
}

if(v==2){
document.forms["form1"]["textarea2"].value+=str
}

}
//-->
</script>


<form name="form1" action="">
<textarea name="textarea1" rows="10" onclick="chk(1)"></textarea>
<textarea name="textarea2" rows="10" onclick="chk(2)"></textarea>
<a href="#null" onclick="insert('<img src=&quot;picture1.gif&quot; alt=&quot;picture1.gif&quot;>')">picture1.gif</a>
<a href="#null" onclick="insert('Hello')">Hello</a>
<a href="#null" onclick="insert('World')">World</a>
</form>

roxii
04-20-2005, 08:15 PM
Thanks for the help. It works great, AND I understand how it works :D

I never realized how similar javascript is to PHP.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum