Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-19-2005, 08:07 PM   PM User | #1
roxii
New Coder

 
Join Date: Apr 2005
Posts: 84
Thanks: 0
Thanked 0 Times in 0 Posts
roxii is an unknown quantity at this point
Exclamation Insert Text Into One of Multiple Textareas

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?

PHP Code:
<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?
roxii is offline   Reply With Quote
Old 04-19-2005, 09:45 PM   PM User | #2
Mr J
Senior Coder

 
Join Date: Aug 2002
Location: UK
Posts: 2,789
Thanks: 2
Thanked 14 Times in 14 Posts
Mr J is on a distinguished road
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>
__________________
The silent one.

The most dangerous thing in the world is an idea.
The most dangerous person in the world is the one with an idea.
Mr J is offline   Reply With Quote
Old 04-19-2005, 10:46 PM   PM User | #3
roxii
New Coder

 
Join Date: Apr 2005
Posts: 84
Thanks: 0
Thanked 0 Times in 0 Posts
roxii is an unknown quantity at this point
Quote:
Originally Posted by Mr J
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.
roxii is offline   Reply With Quote
Old 04-20-2005, 01:56 PM   PM User | #4
Mr J
Senior Coder

 
Join Date: Aug 2002
Location: UK
Posts: 2,789
Thanks: 2
Thanked 14 Times in 14 Posts
Mr J is on a distinguished road
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>
__________________
The silent one.

The most dangerous thing in the world is an idea.
The most dangerous person in the world is the one with an idea.
Mr J is offline   Reply With Quote
Old 04-20-2005, 08:15 PM   PM User | #5
roxii
New Coder

 
Join Date: Apr 2005
Posts: 84
Thanks: 0
Thanked 0 Times in 0 Posts
roxii is an unknown quantity at this point
Thanks for the help. It works great, AND I understand how it works

I never realized how similar javascript is to PHP.
roxii is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:39 AM.


Advertisement
Log in to turn off these ads.