JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsWeb DesignJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old August 31st, 2003, 03:44 PM
johannloubser johannloubser is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Posts: 12 johannloubser User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question manipulating text box with JavaScript

hi here's my problem :
i have a form with a text box. i need a way to make buttons and when they are clicked they insert html tags into the text box.
this is for someone who does not know html. eg. to make a new paragraph they click the 'paragraph' button and it inserts the<p>
tag...

1) is this possible with JavaScript

2) anyone know of any good JS tutorials that would explain this.

thanks

Reply With Quote
  #2  
Old August 31st, 2003, 04:23 PM
terminal's Avatar
terminal terminal is offline
nx
Dev Shed Novice (500 - 999 posts)
 
Join Date: Aug 2003
Location: USA
Posts: 626 terminal Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 5 h 28 m 37 sec
Reputation Power: 0
Send a message via AIM to terminal
heres one thats a bit pumped up...

Code:
<HTML>
<HEAD>
<script type="text/javascript">
function para()
{
document.getElementById('Text').value=document.getElementById('Text').value+"<p>";
}
function sreturn()
{
document.getElementById('Text').value=document.getElementById('Text').value+"<br>";
}
aa=0;
function boldIt()
{
if(aa==0)
{
aa=1;
document.getElementById('Text').value=document.getElementById('Text').value+"<b>";
document.getElementById('Bold').value="End Bold Text";
}
else
{
aa=0;
document.getElementById('Text').value=document.getElementById('Text').value+"</b>";
document.getElementById('Bold').value="Start Bold Text";
}
}
bb=0;
function italicsIt()
{
if(bb==0)
{
bb=1;
document.getElementById('Text').value=document.getElementById('Text').value+"<i>";
document.getElementById('Italics').value="End Italics Text";
}
else
{
bb=0;
document.getElementById('Text').value=document.getElementById('Text').value+"</i>";
document.getElementById('Italics').value="Start Italics Text";
}
}
cc=0;
function underlineIt()
{
if(cc==0)
{
cc=1;
document.getElementById('Text').value=document.getElementById('Text').value+"<u>";
document.getElementById('Underline').value="End Underlined Text";
}
else
{
cc=0;
document.getElementById('Text').value=document.getElementById('Text').value+"</u>";
document.getElementById('Underline').value="Start Underlined Text";
}
}
function resetIt()
{
document.getElementById('Text').value="";
}
</script>
</head>
<body>
<textarea id="Text" cols="50" rows="10"></textarea>
<p>
<input type="button" onClick="para()" value="New Paragraph">
<input type="button" onClick="sreturn()" value="Single Return">
<br>
<input id="Bold" type="button" onClick="boldIt()" value="Start Bold Text">
<input id="Italics" type="button" onClick="italicsIt()" value="Start Italics Text">
<input id="Underline" type="button" onClick="underlineIt()" value="Start Underlined Text">
<br>
<input type="button" onClick="resetIt()" value="Reset">
</BODY>
</HTML>

ill give you an explanation shortly afterwards

Reply With Quote
  #3  
Old August 31st, 2003, 04:49 PM
terminal's Avatar
terminal terminal is offline
nx
Dev Shed Novice (500 - 999 posts)
 
Join Date: Aug 2003
Location: USA
Posts: 626 terminal Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 5 h 28 m 37 sec
Reputation Power: 0
Send a message via AIM to terminal
Talking heres where ill break it down for ya...

Code:
<HTML>
<HEAD>
<script type="text/javascript">
function para()
{
document.getElementById('Text').value=document.getElementById('Text').value+"<p>";
}

this right here is the function to enter the <p> tags. the first string, right here:

document.getElementById('Text').value=document.getElementById('Text').value+"<p>";

the parts before the equal sign say this is common language:
"..we first take the current value of the element with an ID of 'Text'.."

and the parts after:
"..we then change the value of 'Text' to itself plus the addition of the tag '<p>'.."

and so theres your basic setup for your tag insertions.
and for the next on, the same, except we just add different tags:

function sreturn()
{
document.getElementById('Text').value=document.getElementById('Text').value+"<br>";
}

were adding '<br>' tags here.

heres is the really tricky part that may confuse you...
here is the code part that contains the bold, italics, and underline tag insertions. this is hard because these tags require a starter tag(<b>,<i>,<u>) and an ending tag(</b>,</i>,</u>).
in order to explain this to you, ill give you a basic code structure example of setting this up:
Code:
xx=0; //xx's value becomes 0
function function_name()
{
if(xx==0)
{
xx=1; //we change xx's value to 1
/*Do some other stuff while xx's value is still 1*/
}
else
{
xx=0; //we reset xx's value back to 0 so when the function is called again, it can loop back to the beginning
/*Do different things while xx's value is back at 0*/
}
}


since im assuming you prolly dont get it yet, ill break it down for you even further:

ok, first, we declare xx's value to be 0 even before we define the function. then, we give the function a name and create an if..else loop. the if statement is like this:
if(xx==0){}
and so, its saying if the arguements contained within the ()'s are true, then do the following procedures within the {}'s.
and so, since we first declared xx's value to be 0, it enters the loop since the arguements are returned as being true. so, the script navigates to the stuff within the {}'s and executes them one at a time. the first statement we put in there is this:
xx=1;
so, when it enters the if part of the loop, xx's value then becomes 1. and then, after its value is 1, the rest of the procedures are executed.

after the statements completion, it then returns to the beginning and runs through again when the function is called a 2nd time. this time, since xx's value is still 1, the if part returns false. with this, the script then skips down to the else{} area and executes those statements.
our first one resets xx's value to 0, and the rest is executed accordingly. we want to set xx's value back to 0 so the loop can execute again when the function is called a 3rd time.
and there you have it.

now, for working with the live example:

aa=0;

we declare aa's value to be 0.

function boldIt()
{

we define the functions name as boldIt.

if(aa==0)
{

we initialize our if statement and say:
"..if aa is equal to '0', then execute the following procedures.."

aa=1;
document.getElementById('Text').value=document.getElementById('Text').value+"<b>";
document.getElementById('Bold').value="End Bold Text";
}

we set our procedures as so(in plain english):
"..first off, aa should now become 1. then, take the current value of Text and add '<b>' to it. then, change the value of our button with an ID of Bold to 'End Bold Text'.."

else
{
aa=0;
document.getElementById('Text').value=document.getElementById('Text').value+"</b>";
document.getElementById('Bold').value="Start Bold Text";
}
}

after returning to the top upon the function being called a 2nd time, the function skips down to the else part since the if part evaluated to false.
and this is what it says in plain english:
"..first we reset aa's value back to 0 so we can recall the function a 3rd time. then, we take the current value of Text and add '</b>' to it. then, we reset the button Bold's value back to 'Start Bold Text'.."

and so, we have your function for adding paragraphs and carriage returns, and we also have our sophisticated bold, italic, and underline function. now we just have our reset function left.

this functions isnt even close to the others in difficulty. i believe even a newbie to js could easily pick up this function:

function resetIt()
{

we name our function resetIt

document.getElementById('Text').value="";
}

we say that when this function is called, we want the current value of Text to return back to nothing(aka " ").

i hope this helps you out. also, it helps alot to read the explanation while also viewing the actually page.


[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[ TeRmInAl ]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]

Reply With Quote
  #4  
Old September 1st, 2003, 01:05 PM
johannloubser johannloubser is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Posts: 12 johannloubser User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Talking

thanks a lot!!!

Reply With Quote
  #5  
Old September 24th, 2003, 10:28 AM
shockthealien's Avatar
shockthealien shockthealien is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 5 shockthealien User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
How would you do it if you got more than one text areas in the same page that need the formatting tools?

sorry for complicating it a bit more

Reply With Quote
  #6  
Old September 24th, 2003, 11:51 PM
terminal's Avatar
terminal terminal is offline
nx
Dev Shed Novice (500 - 999 posts)
 
Join Date: Aug 2003
Location: USA
Posts: 626 terminal Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 5 h 28 m 37 sec
Reputation Power: 0
Send a message via AIM to terminal
no worries shockthealien. i got that covered here:






EDIT: AHHH shoot....youre not gonna like this, but what i just gave ya guys wont work too well for more than one textarea....blah, i used if's instead of ternaries....so, you can download it if ya want, but im gonna go ahead and change the functions. if ya know what youre doing though, by all means download it and manipulate the functions. sorry again
Attached Files
File Type: html textarea_insert.html (3.4 KB, 393 views)

Last edited by terminal : September 24th, 2003 at 11:59 PM.

Reply With Quote
  #7  
Old September 25th, 2003, 07:41 PM
terminal's Avatar
terminal terminal is offline
nx
Dev Shed Novice (500 - 999 posts)
 
Join Date: Aug 2003
Location: USA
Posts: 626 terminal Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 5 h 28 m 37 sec
Reputation Power: 0
Send a message via AIM to terminal
ok, this one works perfectly. tell me if ya find any bugs though so i can help to clear em up
Attached Files
File Type: html textarea_insert.html (5.0 KB, 374 views)

Reply With Quote
  #8  
Old September 26th, 2003, 03:54 AM
shockthealien's Avatar
shockthealien shockthealien is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 5 shockthealien User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
yup, that works fantastic. Thanx a lot.

Would you hate me if i complicate it a bit more? would it be possible to insert the stuff where the cursor is instead of at the end? or around a selection of text?


Reply With Quote
  #9  
Old September 26th, 2003, 06:28 AM
DmS DmS is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2000
Location: Sweden
Posts: 32 DmS User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 35 sec
Reputation Power: 9
If you like you can use this:
http://www.dmsproject.com/test/wysi...DmSWysiwyg.html
It's an early version that unfortunatley works only in IE, but you can preview the rendered HTML by clicking "GRANSKA".
In this you can highlight a written url click a button and it will create a link from it and so on.
Try it out, please use a link to http://www.dmsproject.com/ if you use it live though.
/Dan

Reply With Quote
  #10  
Old September 26th, 2003, 09:39 PM
terminal's Avatar
terminal terminal is offline
nx
Dev Shed Novice (500 - 999 posts)
 
Join Date: Aug 2003
Location: USA
Posts: 626 terminal Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 5 h 28 m 37 sec
Reputation Power: 0
Send a message via AIM to terminal
hmm...i guess i could try.....might be abit hard....but im up to it

Reply With Quote
  #11  
Old October 9th, 2003, 10:27 AM
shockthealien's Avatar
shockthealien shockthealien is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 5 shockthealien User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
That would be fantstic if you could, cos at the minute is no good for editing text, only for input new text.

any progress so far?


Reply With Quote
  #12  
Old October 11th, 2003, 01:52 AM
terminal's Avatar
terminal terminal is offline
nx
Dev Shed Novice (500 - 999 posts)
 
Join Date: Aug 2003
Location: USA
Posts: 626 terminal Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 5 h 28 m 37 sec
Reputation Power: 0
Send a message via AIM to terminal
hmmm....sorry, no luck yet......however im getting closer as i just examined a script that replaces spaces within a textarea with &nbsp; without replacing or adding it to the very end....so i should be getting closer

Reply With Quote
  #13  
Old October 11th, 2003, 02:45 AM
shockthealien's Avatar
shockthealien shockthealien is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 5 shockthealien User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
sounds promising

Reply With Quote
  #14  
Old January 11th, 2004, 06:08 AM
shockthealien's Avatar
shockthealien shockthealien is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 5 shockthealien User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
any luck?

Reply With Quote
  #15  
Old January 12th, 2004, 09:55 PM
terminal's Avatar
terminal terminal is offline
nx
Dev Shed Novice (500 - 999 posts)
 
Join Date: Aug 2003
Location: USA
Posts: 626 terminal Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 5 h 28 m 37 sec
Reputation Power: 0
Send a message via AIM to terminal
sorry...ive come to believe that its impossible now....
i havent even found a server-side solution yet...
grr...thisll bug the cr@p outta me til i die

[[[terminal]]]

Reply With Quote
Reply

Viewing: Dev Shed ForumsWeb DesignJavaScript Development > manipulating text box with JavaScript


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump



 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT