Replace certain text elements in body content

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • gbit

    #1

    Replace certain text elements in body content

    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>Docume nt Rewrite Example</title>

    <script type="text/javascript">

    function rewritePage() {

    var newval = document.inform .namefield.valu e;

    var txt;
    if (newval.length == 0) {
    txt = document.create TextNode("Gener ic");
    } else {
    txt = document.create TextNode(newval );
    }

    var elem = document.getEle mentById("name" );
    elem.replaceChi ld(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="rewrit ePage()"></input>
    </form>

    <h1> My name is <span id="name">Gener ic</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.

  • Jeff North

    #2
    Re: Replace certain text elements in body content

    On 18 Mar 2005 14:22:25 -0800, in comp.lang.javas cript "gbit"
    <routed2000@yah oo.com> wrote:
    [color=blue]
    >| 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>Docume nt Rewrite Example</title>
    >|
    >| <script type="text/javascript">
    >|
    >| function rewritePage() {
    >|
    >| var newval = document.inform .namefield.valu e;
    >|
    >| var txt;
    >| if (newval.length == 0) {
    >| txt = document.create TextNode("Gener ic");
    >| } else {
    >| txt = document.create TextNode(newval );
    >| }
    >|
    >| var elem = document.getEle mentById("name" );
    >| elem.replaceChi ld(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="rewrit ePage()"></input>[/color]

    <input type="button"

    Then you can remove the form tags.
    [color=blue]
    >| </form>
    >|
    >| <h1> My name is <span id="name">Gener ic</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.[/color]

    ---------------------------------------------------------------
    jnorthau@yourpa ntsyahoo.com.au : Remove your pants to reply
    ---------------------------------------------------------------

    Comment

    • Lee

      #3
      Re: Replace certain text elements in body content

      gbit said:
      [color=blue]
      ><form name="inform">
      >New Name : <input type="text" size="30" name="namefield "></input>
      ><input type="submit" value="Replace" onClick="rewrit ePage()"></input>
      ></form>
      >
      ><h1> My name is <span id="name">Gener ic</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.[/color]

      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.

      Comment

      • gbit

        #4
        Re: Replace certain text elements in body content

        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.getEle mentById('newna me').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.

        Comment

        • McKirahan

          #5
          Re: Replace certain text elements in body content

          "gbit" <routed2000@yah oo.com> wrote in message
          news:1111191101 .372512.225300@ o13g2000cwo.goo glegroups.com.. .[color=blue]
          > 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.getEle mentById('newna me').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.
          >[/color]

          Try

          var newval = document.getEle mentById('newna me').value;


          Comment

          • Lee

            #6
            Re: Replace certain text elements in body content

            gbit said:[color=blue]
            >
            >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.getEle mentById('newna me').nodeValue;
            >
            >And my text field is defined as:
            >
            ><input value="generic" type="text" size="30" id="newname"
            >name="newnam e" />
            >
            >Is there any other way I could get the text entered.[/color]

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

            Comment

            Working...