www.webdeveloper.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 22

Thread: Write the value of a textbox somewhere else.

  1. #1
    Join Date
    Sep 2005
    Posts
    19

    Exclamation Write the value of a textbox somewhere else.

    Hi,

    I want to write a value of a textbox somewhere else in the page, is that possible?
    Im pritty new to this whole javascript thing, but i did some attempts on this one. All i could think about is this:

    Code:
    <input name="mytext" type="text" value="32" />
    <script type="text/javascript">
    document.write(mytext);
    </script>
    Next thing is that i want that value (in this case mytext) in a link. I already tried
    Code:
    <a href='mytext'
    but it didnt work out as well .

    Well hope you can figure it out,

    CyCloneNL

  2. #2
    Join Date
    Apr 2005
    Posts
    387
    I'm sure it's in here somewhere. That site might help you learn

  3. #3
    Join Date
    Sep 2005
    Posts
    19
    Wow thanks, got the first part of the problem solved already. It will write the text i write in my textbox somewhere else.

    Now all i need to know how to get that in a link.

  4. #4
    Join Date
    Apr 2006
    Location
    Perth
    Posts
    154
    You cannot 'easily' write to a page after it has loaded. Much easier to modify the contents of a layer.

    Changing the link 'value' is a matter of assigning a new value to the HREF attribute of an ANCHOR object.

    Code:
    <html>
    <body>
    <form>
    <input type="text" name="someText">
    <input type="button" value="Write" onclick="document.all.link.href=document.getElementById('showText').innerText=this.form.someText.value;">
    </form>
    <div id="showText"></div>
    <a href="" name="link">Variable link</a>
    </body>
    </html>

  5. #5
    Join Date
    Apr 2005
    Posts
    387
    The link text or href?

  6. #6
    Join Date
    Sep 2005
    Posts
    19
    I just want the people to write a site in a textbox and if they click go or something, they will go there.

  7. #7
    Join Date
    Apr 2005
    Posts
    387
    well the simple answer is

    <input type="text" onchange="window.location.href=this.value">

  8. #8
    Join Date
    Apr 2005
    Posts
    387
    Just a note about the site I posted above.

    It's useful for beginners but some questions are old. If you see something with "document.layers" or "document.all" use the modern equivalent document.getElementById('idStr')

  9. #9
    Join Date
    Apr 2006
    Location
    South Carolina
    Posts
    168

    Cool

    try this:

    <html>
    <head>
    </head>

    <body>

    <script type="text/javascript">

    function goToIt() {
    window.location="http://"+document.main.site.value
    }
    </script>


    <form name="main" action="javascript:goToIt()">
    <input name="site" type="text" value="type a site">
    <input type="submit" value="Go!">
    </form>

    </body>
    </html>

  10. #10
    Join Date
    Apr 2005
    Posts
    387
    stephen1davis, just so you know, location is an object.

    Changing the url with window.location is one of those things that works but is incorrect. You need to use the href property of the location object.

  11. #11
    Join Date
    Apr 2006
    Location
    South Carolina
    Posts
    168
    i see- you mean say location.href, but i still don't see what you mean about it being an object, and why isn't it correct?.
    Last edited by stephen1davis; 04-08-2006 at 01:31 PM.

  12. #12
    Join Date
    Apr 2006
    Location
    South Carolina
    Posts
    168

    Cool

    ok- i think i get it. you're saying that by using window.location=, you're not technically setting any properties for the object "location", and you should be setting the property "href". and of course "window" can be dropped anyway. (see, my education in javascript has been a little random...) makes good sense. thanks

  13. #13
    Join Date
    Apr 2005
    Posts
    387
    One of the properties of location is href that can be use to get or set the location. The default property of location is href, so my saying it's "incorrect" is wrong.

    It's like using the keyword "with". It leaves the script open to interpretation by a browser. Better safe than sorry by pointing to the property directly:

    window.location.href=string

    or just

    location.href=string

    because window is assumed

  14. #14
    Join Date
    Sep 2005
    Posts
    19
    Well thanks for all your solutions! You guys reply really fast.

    Can this also be done in a Iframe?

  15. #15
    Join Date
    Apr 2005
    Posts
    387
    Try this

    <input type="text" onchange="top.location.href=this.value">

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles