• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 277
  • Last Modified:

Javascript popup to insert text

Hi

I want to click on a button which then opens a box where I can type in some text, and when i click ok it places that text into a text box on the original page like so:

<click button> (the button needs to be an image)
<insert text "test text">
<click ok>
<display inputted text in the textbox, like: [imgcom]test text[/imgcom]>

Thanks.
0
spikeyjames
Asked:
spikeyjames
  • 5
  • 5
  • 2
1 Solution
 
Michel PlungjanIT ExpertCommented:
For example

<a href="#"
onClick="val = prompt('Enter text');
if (val) document.forms[0].textarea1.value+='[imgcom]'+val+'[/imgcom]';
return false">IMGCOM</a>

<form>
<textarea name="textarea1"></textarea>
</form>
0
 
spikeyjamesAuthor Commented:
whoops sorry I need something slightly different

I need to have a prompt to ask me "what is the image name" and then a prompt to ask me "what is the image comment" and the result will be:

[image]imagename[/image][imgcom]image comment[/imgcom]
0
 
superm401Commented:
<a href="#" onClick="var imgName=prompt('What is the image name?','');var imgCom=prompt('What is the image comment?','');document.forms[0]/textarea1/value+='[image]'+imgName+'[/image][imagecom]'+imgCom+'[/imgcom]');"><img src="imgComButton.gif" border="0"></A>
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
superm401Commented:
Don't forget to have the text box later:

<form>
<textarea name="textarea1"></textarea>
</form>
0
 
Michel PlungjanIT ExpertCommented:
And now without the bugs AND with the return false you will need:



<a href="#"
onClick="imgName = prompt('What is the image name?','');
imgComment = prompt('What is the image comment','');
if (imgName) imgName = '[image]'+imgName+'[/image]';
if (imgComment) imgComment='[imgcom]'+imgComment+'[/imgcom]';
document.forms[0].textarea1.value+=imgName+imgComment;
return false">IMGCOM</a>
<form>
<textarea rows="4" cols="40" name="textarea1"></textarea></form>
0
 
spikeyjamesAuthor Commented:
Thank you very much mplungjan, that's the 2nd time you've helped me.
0
 
spikeyjamesAuthor Commented:
One last thing...If I want to put something with apostrophies in the javascript, how do I get it to ignore them? for example, instead of [image] I want to put <div align="right">
0
 
Michel PlungjanIT ExpertCommented:
<script>
function addTags() {
  var imgName = prompt('What is the image name?','');
  var imgComment = prompt('What is the image comment','');
  if (imgName) imgName = '<div align="right">'+imgName+'"</div>';
  if (imgComment) imgComment='<div align="right">'+imgComment+'</div>';
  document.forms[0].textarea1.value+=imgName+imgComment;
  return false
}
</script>
<a href="#"
onClick="return addTags()">IMGCOM</a>
<form>
<textarea rows="4" cols="40" name="textarea1"></textarea></form>
0
 
spikeyjamesAuthor Commented:
Hmm, it doesn't work because I have to add a bit of PHP code to the textarea1 name, so for example:

 <script>
function addTags() {
  var imgName = prompt('What is the image name?','');
  var imgComment = prompt('What is the image comment','');
  if (imgName) imgName = '<div align="right">'+imgName+'"</div>';
  if (imgComment) imgComment='<div align="right">'+imgComment+'</div>';
  document.forms[0].aTextArea<?php echo $x;?>.value+=imgName+imgComment;
  return false
}
</script>

But the PHP doesn't get parsed inside that code.
0
 
Michel PlungjanIT ExpertCommented:
It should...

how about
document.forms[0].aTextArea<? echo $x; ?>.value

??
0
 
spikeyjamesAuthor Commented:
ah yes, looking at the source it is parsing the PHP, but for some reason it's posting the data into the wrong text box...I shall try and fix this myself, thanks for your help.
0
 
Michel PlungjanIT ExpertCommented:
YW. good night
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 5
  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now