Current location: Hot Scripts Forums » General Web Coding » JavaScript » inserting a line of code on current cursor possition into textarea


inserting a line of code on current cursor possition into textarea

Reply
  #1 (permalink)  
Old 07-26-07, 03:29 AM
sujata_ghosh sujata_ghosh is offline
Wannabe Coder
 
Join Date: May 2006
Posts: 181
Thanks: 0
Thanked 0 Times in 0 Posts
inserting a line of code on current cursor possition into textarea

Hi to all!

I would like to get help on the follow:

On a form i have a textarea where i write data and on while wrtting on that textarea i want that on a button click the following line willm be inserted on current position of the cursor on that field.

<img src="" border="0" align="center">

It can be on any possition. first middle or end on that textarea while i am writting.

can any one help me on that?

Thanks,
Reply With Quote
  #2 (permalink)  
Old 07-26-07, 04:58 PM
TwoD TwoD is offline
Community VIP
 
Join Date: Sep 2003
Location: 404
Posts: 1,813
Thanks: 0
Thanked 0 Times in 0 Posts
There's a good script a few posts down in "Maintaining Cursor Position": http://www.programmingtalk.com/showthread.php?t=39382
__________________
[W3Schools - learn all about the standards.] [QuirksMode - Browser Quirks] [MS's Online Reference Docs] [DOM in Gecko.]
Please pay attention to stickys, announcements and forum rules, thank you.
Please also remember Code Wrappers and [SOLVED] Marking, this helps everyone.
Reply With Quote
  #3 (permalink)  
Old 08-07-07, 12:27 AM
sujata_ghosh sujata_ghosh is offline
Wannabe Coder
 
Join Date: May 2006
Posts: 181
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks TWOD!

I have used that code but is showing me error :

line 59
char: 1
erro: object doesn't support properlty or method.

so please tell me what do i need to pass the value when i am calling the script? i have used whole script into the form and i am calling the function like that:

this is part of the code of whole form:
HTML Code:
<input type="button" name="imagecur" value=" image " onclick="javascript:insertAtCursor('document.vbform.mess','<img src="" border="0" align="center">')" class="submitbutton">
</TD></TR>
 <TR>
    <TD><b>Date:</b> <INPUT size=20 name="datecur" value="$dt"></TD>
    <TD><b>Time:</b> <INPUT size=30 name="time" value="$time"></TD>
 </TR>
  
   <tr>
     <td colspan=2 valign="top"><b>Matter:</b> <br><TEXTAREA name="mess" rows="10" wrap=virtual
    cols=75>$mess</TEXTAREA></td></tr>
and my textarea name is 'mess' and i would like to add this image code into anywhere on this textarea when i click the button above.


is this above will be correct?

Thanks.

Last edited by TwoD; 08-07-07 at 09:12 AM. Reason: Added [html] wrapper
Reply With Quote
  #4 (permalink)  
Old 08-07-07, 09:20 AM
TwoD TwoD is offline
Community VIP
 
Join Date: Sep 2003
Location: 404
Posts: 1,813
Thanks: 0
Thanked 0 Times in 0 Posts
The first argument should be a reference (to an input object/element), not a string.
So remove the quotes in the first argument to make that ok. In the second argument you need to escape the quotes inside the string so they don't end the outer string, like this:
HTML Code:
<input type="button" name="imagecur" value=" image " onclick="javascript:insertAtCursor(document.vbform.mess,'<img src=\"\" border=\"0\" align=\"center\">')" class="submitbutton">
Btw, depending on which HTML version you aim to use, the align attribute might not validate.

Also, I think I saw a post in another forum where someone had problems when trying to change an img tag's src attribute after it was created with an empty src. Putting the url to a 1x1px blank image helped tho. Depending on how you intend to use the image, an alt attribute should also be used. XHTML won't validate without it I think.


Btw, if document.vbform.mess doesn't work, give the textarea the id="mess" and use document.getElementById(\"mess\") there instead.
__________________
[W3Schools - learn all about the standards.] [QuirksMode - Browser Quirks] [MS's Online Reference Docs] [DOM in Gecko.]
Please pay attention to stickys, announcements and forum rules, thank you.
Please also remember Code Wrappers and [SOLVED] Marking, this helps everyone.
Reply With Quote
  #5 (permalink)  
Old 08-08-07, 12:33 AM
sujata_ghosh sujata_ghosh is offline
Wannabe Coder
 
Join Date: May 2006
Posts: 181
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks TwoD!

I have used this code:

Code:
<input type="button" name="imagecur" value=" image " onclick="javascript:insertAtCursor(document.vbform.mess,'<img src= border=0 align=center>')" class="submitbutton">
Code:
<TEXTAREA id="mess" rows="10" wrap=virtual
    cols=75>$mess</TEXTAREA>
I am using this Javascript code into a CGI coded HTML form. and the page is simple HTML page where that align="center" works.

now the problem is i can't able to pass the exact code what i want like this:
<img src="" border="0" align="center">

it not executed the code if it put ' "" ' this and then this line is showing besid the button written on the form as of they are text instead of code.

if i pass the like this:
<img src= border=0 align=center>

then its work. but quotes should be there.

Please suggest what to do next.

Thanks a lot for ur cooperation.
Reply With Quote
  #6 (permalink)  
Old 08-08-07, 10:41 AM
TwoD TwoD is offline
Community VIP
 
Join Date: Sep 2003
Location: 404
Posts: 1,813
Thanks: 0
Thanked 0 Times in 0 Posts
Do you have a testpage online so I can take a look and see why it doesn't work?

The code I posted with the \" should have worked, but you can also try \'
__________________
[W3Schools - learn all about the standards.] [QuirksMode - Browser Quirks] [MS's Online Reference Docs] [DOM in Gecko.]
Please pay attention to stickys, announcements and forum rules, thank you.
Please also remember Code Wrappers and [SOLVED] Marking, this helps everyone.
Reply With Quote
  #7 (permalink)  
Old 08-09-07, 12:22 AM
sujata_ghosh sujata_ghosh is offline
Wannabe Coder
 
Join Date: May 2006
Posts: 181
Thanks: 0
Thanked 0 Times in 0 Posts
yes you can take a look at here:

http://www.k****ij.com/cgi-bin/testfunda1.cgi (without ")

http://www.k****ij.com/cgi-bin/testfunda.cgi (with ")


please replace **** with the word " s h i t "

but make sure u don;t give any space between letters.

Thanks.
Reply With Quote
  #8 (permalink)  
Old 08-09-07, 05:35 AM
TwoD TwoD is offline
Community VIP
 
Join Date: Sep 2003
Location: 404
Posts: 1,813
Thanks: 0
Thanked 0 Times in 0 Posts
Here's the line from the page with the quotes.
HTML Code:
<input name="imagecur" value=" image " onclick="javascript:insertAtCursor(document.vbform.mess,'<img src=" align="middle" border="0" type="button">')" class="submitbutton"&gt;
It should have been
HTML Code:
<input name="imagecur" value=" image " onclick="javascript:insertAtCursor(document.vbform.mess,'<img src=\"\" align=\"middle\" border=\"0\" type=\"button\">')" class="submitbutton">
__________________
[W3Schools - learn all about the standards.] [QuirksMode - Browser Quirks] [MS's Online Reference Docs] [DOM in Gecko.]
Please pay attention to stickys, announcements and forum rules, thank you.
Please also remember Code Wrappers and [SOLVED] Marking, this helps everyone.

Last edited by TwoD; 08-10-07 at 07:40 AM.
Reply With Quote
  #9 (permalink)  
Old 08-10-07, 03:09 AM
sujata_ghosh sujata_ghosh is offline
Wannabe Coder
 
Join Date: May 2006
Posts: 181
Thanks: 0
Thanked 0 Times in 0 Posts
you can take a look into your new code at here:
http://www.k****ij.com/cgi-bin/testfunda.cgi

its not working

i hope you have missed ">" this at the end of '<img' code.

HTML Code:
<input type="button" name="imagecur" value=" image " onclick="javascript:insertAtCursor(document.vbform.mess,'<img src=\"\" border=\"0\" align=\"center\" tupe=\"button\"')" class="submitbutton">
please check it.

anyway i have tried this also:
HTML Code:
<input type="button" name="imagecur" value=" image " onclick="javascript:insertAtCursor(document.vbform.mess,'<img src=\"\" border=\"0\" align=\"center\" tupe=\"button\">')" class="submitbutton">
 
but result is same it was before, mean last time.

thanks.

Last edited by Nico; 08-10-07 at 03:22 AM.
Reply With Quote
  #10 (permalink)  
Old 08-10-07, 07:45 AM
TwoD TwoD is offline
Community VIP
 
Join Date: Sep 2003
Location: 404
Posts: 1,813
Thanks: 0
Thanked 0 Times in 0 Posts
Yes, I forgot the >
But you keep forgetting the backslashes before the quotes on the page.
I can see you included them here, but when I view source in IE on that last page you posted, I get:
HTML Code:
<input type="button" name="imagecur" value=" image " onclick="javascript:insertAtCursor(document.vbform.mess,'<img src="" border="0" align="center" type="button"')" class="submitbutton">
Which seems kinda weird. I also get a different line if I save the page in FF. (FF messes with the code if you view source, but saving the page should leave it as it is.)
__________________
[W3Schools - learn all about the standards.] [QuirksMode - Browser Quirks] [MS's Online Reference Docs] [DOM in Gecko.]
Please pay attention to stickys, announcements and forum rules, thank you.
Please also remember Code Wrappers and [SOLVED] Marking, this helps everyone.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Alphabetising Line add to Gallery Code VKX PHP 1 10-04-05 09:18 AM
Redirection back to a page from form submit DAL Perl 11 03-21-05 02:45 PM
How to display from $a line to $z line ? websnow PHP 6 12-22-04 05:34 PM
this line of code npsken PHP 1 08-16-04 08:13 PM
asp-iis-Server error nsuresh_rasr ASP.NET 3 02-08-04 12:47 AM


All times are GMT -5. The time now is 11:27 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.