SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Feb 2005
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    change html-text with a js-function

    Hi,

    I want a js-function which changes the text of a html-textfield.
    How can I do this?

    Thanx...

    p-flash

  2. #2
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Portsmouth UK
    Posts
    1,498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    lots of ways to do this

    a couple

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

    <
    html>

    <
    head>
      <
    title></title>
    <
    script language="JavaScript" type="text/javascript">
    <!--
    function 
    CngName(m){
     
    newname=window.prompt('Hi\nEnter your name below','');
     
    newname='My Name is '+newname
     
    if (m){
      
    document.getElementById('Example').value=newname;
     }
     else {
      
    document.Fred.Example.value=newname;
     }

    }
    //-->
    </script></head>

    <body>
    <form name="Fred" >
    <input id="Example" name="Example" value="My Name is Fred">
    </form>
    <br>
    <input type="button" name="" value="Method 1 using 'name'"
     onclick="javascript:CngName(0);"
     style="width:200px;font-size:12px;"
     >
     <br>
    <input type="button" name="" value="Method 2 using 'getElementById'"
     onclick="javascript:CngName(1);"
     style="width:200px;font-size:12px;" >
     <br>
    <input type="button" name="" value="My Name is Tom with no prompt"
     onclick="javascript:document.getElementById('Example').value='My Name is Tom';"
     style="width:200px;font-size:12px;" >
     <br>
    </body>

    </html> 

  3. #3
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can set the value of a textbox by setting its .value property to some text:

    document.getElementById("elmtID").value = "hello world";

  4. #4
    SitePoint Member
    Join Date
    Feb 2005
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you, it works!

    p-flash

  5. #5
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    another way is

    <head>
    <script language="Javascript">
    function setText(){
    var Text = prompt('Please insert your text','');
    if (Text) {
    test.innerHTML = Text;
    }
    else
    {
    test.innerHTML = "No text defined";
    }
    }

    </script>
    </head>
    <body onLoad="setText();">
    <span id="text"></span>

    What this will do is ask for what text to use, then inserts it, if none is given then it will set another one. and there are no text boxes to be seen, this is directly inserted into the page.

    Gav


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •