|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 |
|
#2
|
||||
|
||||
|
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 |
|
#3
|
||||
|
||||
|
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 ]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]] |
|
#4
|
|||
|
|||
|
thanks a lot!!!
|
|
#5
|
||||
|
||||
|
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 |
|
#6
|
||||
|
||||
|
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 Last edited by terminal : September 24th, 2003 at 11:59 PM. |
|
#7
|
||||
|
||||
|
ok, this one works perfectly. tell me if ya find any bugs though so i can help to clear em up
|
|
#8
|
||||
|
||||
|
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? ![]() |
|
#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 |
|
#10
|
||||
|
||||
|
hmm...i guess i could try.....might be abit hard....but im up to it
|
|
#11
|
||||
|
||||
|
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? ![]() |
|
#12
|
||||
|
||||
|
hmmm....sorry, no luck yet......however im getting closer as i just examined a script that replaces spaces within a textarea with without replacing or adding it to the very end....so i should be getting closer
|
|
#13
|
||||
|
||||
sounds promising |
|
#14
|
||||
|
||||
|
any luck?
|
|
#15
|
||||
|
||||
|
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]]] |
![]() |
| Viewing: Dev Shed Forums > Web Design > JavaScript Development > manipulating text box with JavaScript |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|