By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,776 Members | 1,292 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,776 IT Pros & Developers. It's quick & easy.

Replace certain text elements in body content

P: n/a
Hi,

I have a web page describing a procedure using generic names. At the
top
of the page I have a text box. When the user enters a specific name in
the
text box, I would like the page to redisplay the procedure using the
name
entered in the box. For that I started like this:

<html>
<head>
<title>Document Rewrite Example</title>

<script type="text/javascript">

function rewritePage() {

var newval = document.inform.namefield.value;

var txt;
if (newval.length == 0) {
txt = document.createTextNode("Generic");
} else {
txt = document.createTextNode(newval);
}

var elem = document.getElementById("name");
elem.replaceChild(txt, elem.firstChild);
}

</script>
</head>

<body bgcolor="white">

<form name="inform">
New Name : <input type="text" size="30" name="namefield"></input>
<input type="submit" value="Replace" onClick="rewritePage()"></input>
</form>

<h1> My name is <span id="name">Generic</span> </h1>

</body>
</html>

Problem is the name changes for a moment, but it changes back to the
original
value after the document is loaded completly.

How can I fix this.

--
...Senthil.

Jul 23 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On 18 Mar 2005 14:22:25 -0800, in comp.lang.javascript "gbit"
<ro********@yahoo.com> wrote:
| Hi,
|
| I have a web page describing a procedure using generic names. At the
| top
| of the page I have a text box. When the user enters a specific name in
| the
| text box, I would like the page to redisplay the procedure using the
| name
| entered in the box. For that I started like this:
|
| <html>
| <head>
| <title>Document Rewrite Example</title>
|
| <script type="text/javascript">
|
| function rewritePage() {
|
| var newval = document.inform.namefield.value;
|
| var txt;
| if (newval.length == 0) {
| txt = document.createTextNode("Generic");
| } else {
| txt = document.createTextNode(newval);
| }
|
| var elem = document.getElementById("name");
| elem.replaceChild(txt, elem.firstChild);
| }
|
| </script>
| </head>
|
| <body bgcolor="white">
|
| <form name="inform">
| New Name : <input type="text" size="30" name="namefield"></input>
| <input type="submit" value="Replace" onClick="rewritePage()"></input>
<input type="button"

Then you can remove the form tags.
| </form>
|
| <h1> My name is <span id="name">Generic</span> </h1>
|
| </body>
| </html>
|
| Problem is the name changes for a moment, but it changes back to the
| original
| value after the document is loaded completly.
|
| How can I fix this.


---------------------------------------------------------------
jn******@yourpantsyahoo.com.au : Remove your pants to reply
---------------------------------------------------------------
Jul 23 '05 #2

P: n/a
Lee
gbit said:
<form name="inform">
New Name : <input type="text" size="30" name="namefield"></input>
<input type="submit" value="Replace" onClick="rewritePage()"></input>
</form>

<h1> My name is <span id="name">Generic</span> </h1>

</body>
</html>

Problem is the name changes for a moment, but it changes back to the
original
value after the document is loaded completly.

How can I fix this.


Don't use a submit control if you don't really want to submit it.
What's happening is that the text is being rewritten by the onClick
event handler, but then the page is being submitted (to itself,
since you don't specify any other action). Use a button, instead.

Jul 23 '05 #3

P: n/a
Thanks. That worked. But I ran into another problem. If I remove the
form, my text field input
doesn't work.

I tried using:

var newval = document.getElementById('newname').nodeValue;

And my text field is defined as:

<input value="generic" type="text" size="30" id="newname"
name="newname" />

Is there any other way I could get the text entered.

Thanks.

Jul 23 '05 #4

P: n/a
"gbit" <ro********@yahoo.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
Thanks. That worked. But I ran into another problem. If I remove the
form, my text field input
doesn't work.

I tried using:

var newval = document.getElementById('newname').nodeValue;

And my text field is defined as:

<input value="generic" type="text" size="30" id="newname"
name="newname" />

Is there any other way I could get the text entered.

Thanks.


Try

var newval = document.getElementById('newname').value;
Jul 23 '05 #5

P: n/a
Lee
gbit said:

Thanks. That worked. But I ran into another problem. If I remove the
form, my text field input
doesn't work.

I tried using:

var newval = document.getElementById('newname').nodeValue;

And my text field is defined as:

<input value="generic" type="text" size="30" id="newname"
name="newname" />

Is there any other way I could get the text entered.


Don't get rid of the form, just the submit button.

Jul 23 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.