473,387 Members | 1,542 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Is it possible to have rewrite html formatted text from a button click in javascript?

My daughter is playing around trying to learn JavaScript and she wrote
a small program that prints out a message in increasing and decreasing
font size and color changes. She is using document write and it works
fine until she puts it into a function and then calls the function from
a button with onClick. This also seems to work OK, with the exception
that the page is cleared, and the curser changes (and stays) as an
hourglass.

In reading some threads it seems that after the page is loaded,
subsequent calls to document.write cause the page to clear, so only
output is written and the button is lost.

In essence is there a way to do the following:

Have a input text filed and a button. Clicking the button writes out
formatted html below it. Clicking it again rewrites it out with new
values depending on the value in the input text.

Any ideas on how to do this easily would be appreciated.

Thanks in advance.

---Jay

Here is the code she wrote:

<html>
<script type="text/javascript">

function coolJulie (firstval, big, small){

var i = firstval
while (i < big) {
document.write("<center><font size= "+ i +" color=" + i * i * i * i *
109684 + ">You Just Typed in: " + document.myForm.hi.value +
"</center></font><BR>");
i = i + 1
}
while (i > small) {
document.write("<center><font align=center size= "+ i +" color=" + i
* i * i * i * 109684 + ">You Just Typed in: " +
document.myForm.hi.value + "</center></font><BR>");
i = i - 1
}
}

</script>

<body>
<form name='myForm'>
<br><br>
<center>
<b>
<font size=3 color='red'>Hi I'm so cooler then some people, maybe even
you!</font>
<br>
<input type=text name='hi' value='yoohoo'>
<input type=button name="ClickMe" value= 'Click Here if you're done
typing' onClick='coolJulie(1,5,1);'>

</form>
</body>
</html>

Jul 23 '05 #1
4 3384
Hello

You might want to play with innerHTML.

<html>
<head>
<title> New Document </title>
<meta name="Generator" content="EditPlus">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
</head>
<script language="JavaScript">
<!--
function getElement(id)
{
if (document.getElementById)
return document.getElementById(id);
else if (document.all)
return document.all[id];
else
return document.layers[id];
}

function generateCoolJulie(firstval, big, small, thetext)
{
var i = firstval;
var s = '';
for (i=firstval;i<big;i++)
{
s = s + "<center><font size= "+ i +" color=" + (i * i * i * i *
109684) + ">You Just Typed in: "
+ thetext
+ "</center></font><BR>";
}

while (i > small)
{
s = s + "<center><font align=center size= " + i +" color="
+ (i * i * i * i * 109684) + ">You Just Typed in: "
+ thetext
+ "</center></font><BR>";
i = i - 1
}

return s;
}

function changeMe(newText)
{
var o = getElement('hey');
o.innerHTML = newText;
}

//easeb

//-->
</script>
<body>
<hr>
<span id="hey">Hello World!</span>
<hr>
<form>
Enter text: <input type="text" name="thetext" value="The text"><br>
<input type="button" value="change!"
onclick="changeMe(generateCoolJulie(1,5,1, this.form.thetext.value));">
</form>
</body>
</html>
<fr********@yahoo.com> wrote in message
news:11*********************@g44g2000cwa.googlegro ups.com...
My daughter is playing around trying to learn JavaScript and she wrote
a small program that prints out a message in increasing and decreasing
font size and color changes. She is using document write and it works
fine until she puts it into a function and then calls the function from
a button with onClick. This also seems to work OK, with the exception
that the page is cleared, and the curser changes (and stays) as an
hourglass.

In reading some threads it seems that after the page is loaded,
subsequent calls to document.write cause the page to clear, so only
output is written and the button is lost.

In essence is there a way to do the following:

Have a input text filed and a button. Clicking the button writes out
formatted html below it. Clicking it again rewrites it out with new
values depending on the value in the input text.

Any ideas on how to do this easily would be appreciated.

Thanks in advance.

---Jay

Here is the code she wrote:

<html>
<script type="text/javascript">

function coolJulie (firstval, big, small){

var i = firstval
while (i < big) {
document.write("<center><font size= "+ i +" color=" + i * i * i * i *
109684 + ">You Just Typed in: " + document.myForm.hi.value +
"</center></font><BR>");
i = i + 1
}
while (i > small) {
document.write("<center><font align=center size= "+ i +" color=" + i
* i * i * i * 109684 + ">You Just Typed in: " +
document.myForm.hi.value + "</center></font><BR>");
i = i - 1
}
}

</script>

<body>
<form name='myForm'>
<br><br>
<center>
<b>
<font size=3 color='red'>Hi I'm so cooler then some people, maybe even
you!</font>
<br>
<input type=text name='hi' value='yoohoo'>
<input type=button name="ClickMe" value= 'Click Here if you're done
typing' onClick='coolJulie(1,5,1);'>

</form>
</body>
</html>

Jul 23 '05 #2
Thanks lallous,

After fixing some line break problems from the newreader, it worked
like a charm and it will be something I can easily explain.

---Jay

Jul 23 '05 #3
fr********@yahoo.com wrote:
My daughter is playing around trying to learn JavaScript and she wrote
a small program that prints out a message in increasing and decreasing
font size and color changes. She is using document write and it works
fine until she puts it into a function and then calls the function from
a button with onClick. This also seems to work OK, with the exception
that the page is cleared, and the curser changes (and stays) as an
hourglass.

In reading some threads it seems that after the page is loaded,
subsequent calls to document.write cause the page to clear, so only
output is written and the button is lost.

In essence is there a way to do the following:

Have a input text filed and a button. Clicking the button writes out
formatted html below it. Clicking it again rewrites it out with new
values depending on the value in the input text.

Any ideas on how to do this easily would be appreciated.

Thanks in advance.

---Jay

Here is the code she wrote:
If she is going to learn, she may as well get it right from the start.
One of the most important things to get right is validating the HTML
source code - there is a free validator at w3.org:

<URL:http://validator.w3.org/>

The page that was posted will cause a series of errors, but once you
fix them you'll know that your page is now a much, much better piece of
work.

<html>
You should always include a doctype. Browsers may display your page
differently depending on the doctype specified. Without one, you are
trusting to luck. Since HTML 4.01 has been around for 5 years now, it
seems reasonable to use that:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">



<script type="text/javascript">
<head> tags aren't mandatory, but it's always handy to have them.
Browsers will insert a head element where they think it's appropriate,
but why leave it to luck that they'll be in the right place? A
<title> element is required, it's generally good to put them in right
below the opening HTML tag:

<html>
<head>
<title> new document </title>
<meta http-equiv="Content-Type"
content="text/html; charset=ISO-8859-1">
<script type="text/javascript">

function coolJulie (firstval, big, small){
var i = firstval
while (i < big) {
document.write("<center><font size= "+ i +" color=" + i * i * i * i *
When you write into the document, you should use good quality HTML.
'font' is depreciated, so use 'style' instead.

The intention seems to be to increase the size of the font by some
increment. Rather than the old font sizes, percentages can be used
based on steps of say 5%, so font-size can be increased:

font-size = 100 + ( i*5) + '%';

Similarly for the colour, which is specified with three values, one
each for red, green and blue (rgb) in a range from 0 to 255. The usual
way is to use hexidecimal values (#a3f523 or such) but to save a lot of
work, we can use rgb(x, y, z) instead, e.g. rgb(255,0,0) will be red.

Here is some code that creates some random colours for the text:

var red = Math.floor(Math.random()*256);
var green = Math.floor(Math.random()*256);
var blue = Math.floor(Math.random()*256);
109684 + ">You Just Typed in: " + document.myForm.hi.value +
"</center></font><BR>");
So now the script so far looks like:

function coolJulie (firstval, big, small){
var i = firstval
while ( i < big ) {
var fontsize = 100 + ( i*5) + '%';
var red = Math.floor(Math.random()*256);
var green = Math.floor(Math.random()*256);
var blue = Math.floor(Math.random()*256);
document.write('<p style="',
'font-size: ' + fontsize + ';',
'color: ' + 'rgb(' + red +',' + green +',' + blue + ');',
'text-align: center;',
'">',
'You Just Typed in: ' + txt,
'</p>');
i = i + 1
This can be written:

i++;
}
while (i > small) {
This one can be the reverse of the above:

while ( i > small ) {
var fontsize = 100 + ( i*5) + '%';
var red = Math.floor(Math.random()*256);
var green = Math.floor(Math.random()*256);
var blue = Math.floor(Math.random()*256);
document.write('<p style="',
'font-size: ' + fontsize + ';',
'color: ' + 'rgb(' + red +',' + green +',' + blue + ');',
'text-align: center;',
'">',
'You Just Typed in: ' + txt,
'</p>');
i--;
}

[...]
<body>
<form name='myForm'>
<br><br>
<center>
<b>
<font size=3 color='red'>Hi I'm so cooler then some people, maybe even
you!</font>
<br>
<input type=text name='hi' value='yoohoo'>
<input type=button name="ClickMe" value= 'Click Here if you're done
typing' onClick='coolJulie(1,5,1);'>
When putting quotes inside other quotes, you need to be careful. You
can put single quotes inside doubles, or doubles inside singles, but
beyond that it gets more complicated:

<input type=button name="ClickMe" value="Click Here if you're done
typing" onClick="coolJulie(1,5,1);">

I like to use doubles in HTML and single in JavaScript, but it's up to
you.

</form>
</body>
</html>


Since you don't want to delete your document content all the time, you
could use innerHTML to write to the page, but using the document object
model is easier, you write to the elements rather than changing the
underlying HTML source. It's also better to split out some of the
logic into separate functions. Here's a solution:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title> Coloured text </title>
<meta http-equiv="Content-Type"
content="text/html; charset=ISO-8859-1">

<script type="text/javascript">

// This writes the paragraph to the page in random colours:
function writePara( fsize, txt, tgt ) {
var oPara = document.createElement('P'); // Create a P element
var red = Math.floor(Math.random()*256); // random red value
var green = Math.floor(Math.random()*256); // random green value
var blue = Math.floor(Math.random()*256); // random blue value
oPara.style.color = 'rgb(' + red +',' + green + ',' + blue + ')';
oPara.style.textAlign = 'center'; // center-align the P
oPara.style.fontSize = fsize; // set the size of the text
var oTxt = document.createTextNode(txt); // add the text
oPara.appendChild(oTxt); // add the text to the P
tgt.appendChild(oPara); // add the P to the document
}

// This removes the content of an element:
function deleteChildren( tgt ){
while ( tgt.firstChild ) {
tgt.removeChild(tgt.firstChild);
}
}

// This decides what to write where:
function coolJulie(firstval, big, small){
var i = firstval;
var txt = document.myForm.hi.value;
var tgt = document.getElementById('julieDiv');
deleteChildren(tgt);
while ( i < big ) {
var fontsize = 100 + ( i*10) + '%';
writePara( fontsize, txt, tgt);
i++;
}
while ( i >= small ) {
var fontsize = 100 + ( i*30) + '%';
writePara( fontsize, txt, tgt);
i--;
}
}
</script>
<body>
<form name="myForm" action="">
<div style="text-align: center; color: red; font-weight: bold;
background-color: white;
">Hi I'm so cooler then some people, maybe even you<br>
<input type="text" name="hi" value="yoohoo">
<input type="button" name="ClickMe"
value="Click here when you're finished typing"
onclick="coolJulie(1,5,1);">
</div>
</form>
<div id="julieDiv"></div>

</body>
</html>


--
Rob
Jul 23 '05 #4
JRS: In article <11*********************@g44g2000cwa.googlegroups. com>,
dated Sat, 25 Jun 2005 19:03:59, seen in news:comp.lang.javascript,
fr********@yahoo.com posted :

In essence is there a way to do the following:

Have a input text filed and a button. Clicking the button writes out
formatted html below it. Clicking it again rewrites it out with new
values depending on the value in the input text.

Any ideas on how to do this easily would be appreciated.


Take a copy of <URL:http://www.merlyn.demon.co.uk/js-quick.htm>.

Insert Div.innerHTML = "A<big>B<big>C<big>D</big>E</big>F</big>G"
in the textarea F.Code; press the "Eval" button.

Then simplify so that eval is not used and the button directly executes
Div.innerHTML = F.Code.value

Then remove all else that you do not need, and read FAQ 4.49 & notes.

Alternatively, see FAQ 4.15.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: KathyB | last post by:
Hi, I'm trying to find a way to validate input text boxes where I don't know the names until the page is rendered. I've got 2 validate functions that fire with the onsubmit button of a "mini" form...
6
by: Stefan Mueller | last post by:
The following code (HTML) generates a table. Now I'd like to insert a new row by a javascript. The following code (javascript) works with the Internet Explorer and also with Mozilla. However, the...
17
by: Lloyd Sheen | last post by:
This IDE is driving me nuts. I needed another button so I copied an existing one, changed the Text and the id and position by drag and drop. Well then I run and get the following: Control...
3
by: Luqman | last post by:
How can I retrieve text from html file and write to textbox. Best Regards, Luqman
9
by: Neo Geshel | last post by:
I have strip-mined, strip-searched, and completely exhausted the Internet (up to the 30th page on Google, with 100 results per page!!), all without finding an answer to my question AS TO WHY IT...
29
by: Richard Lionheart | last post by:
Hi All, I've taken the advice of a few people and managed to cobble together an HTML 4.01 Strict-compliant document (according to the W3C Validation Service), but the way I try to pass a...
21
by: Ben | last post by:
Hello I have frames set up in an asp.net application and need one frame to refresh another. Seeing as events need to be registered at the time the page is sent from the server, I was wondering...
0
by: bruce | last post by:
hi... it appears that i'm running into a possible problem with mechanize/browser/python rgarding the "select_form" method. i've tried the following and get the error listed: br.select_form(nr...
2
by: Sreenath Rao Nellutla | last post by:
Hai all, I am trying to create dropdown calendar control with HTML input control by writing JavaScript. But while executing I am getting the error as "Error on Page" on the status bar of the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.