The new forums will be named Coin Return (based on the most recent vote)! You can check on the status and timeline of the transition to the new forums here.
The Guiding Principles and New Rules document is now in effect.

Javascript: Replacing text inputs/textareas with their content

SeguerSeguer of the VoidSydney, AustraliaRegistered User regular
edited December 2007 in Help / Advice Forum
Hey guys,

Just wondering if any one is aware of a relatively easy way to loop through all input elements of type="text" and textareas, and then replace that html part of them with a span that contains the text in that element. Possibly a way to turn them back as well, though that's a bit more difficult.

I've been tasked with making a printable form using HTML (which is done), and I'm trying to make it print nicely. Printing the inputs is fine with a print css to remove the borders etc, but if there's an easy way of doing this, then it is going to look the best, over different browsers as well.

I already have a way to loop through the elements and I know you can write dynamic html, but I'd need the parent element etc.

Seguer on

Posts

  • devoirdevoir Registered User regular
    edited December 2007
    If I'm understanding you correctly, I've done a similar thing where I put the span around the actual element and replace the element's content HTML with whatever I want.

    So what you do is have something like a span with an id of "container_01" and inside that an input with an id of "content_01", pull the text from "content_01" and replace the content of "container_01" with that text.

    devoir on
  • life3life3 Registered User regular
    edited December 2007
    You want to use the innerHTML property.

    devoir outlined is pretty well. In your HTML you've got:
    <span id="thespan"><textarea blah blah blah/></span>
    

    Then in your javascript you'd get your "thespan" object through whatever means(loop through, getElementByID, etc..).
    thespanobject.innerHTML = "<a href='http://alinkinsteadoftextbox.com'>yeay!</a>"
    

    life3 on
    HOW APPROPRIATE [URL="aim:goim?screenname=skullc0rp"]YOU[/URL] FIGHT LIKE A COW
  • SeguerSeguer of the Void Sydney, AustraliaRegistered User regular
    edited December 2007
    Thanks, that's pretty much what I was aiming for. After a bit more print testing I don't need it for inputs, just textareas, which will simplify things.

    Seguer on
  • JasconiusJasconius sword criminal mad onlineRegistered User regular
    edited December 2007
    If you have multiple text fields to deal with, you can give them a common name attribute, and there is a javascript function that will put all of those text fields into an array as objects.

    Then you can just loop through the array and run innerHTML on them.

    That's my method of choice when dealing with multiple items.

    getElementsByName is handy, because as far as I know it's the only one that will store it an array.

    Jasconius on
    this is a discord of mostly PA people interested in fighting games: https://discord.gg/DZWa97d5rz

    we also talk about other random shit and clown upon each other
  • SeguerSeguer of the Void Sydney, AustraliaRegistered User regular
    edited December 2007
    Thanks Jasconius, I'll give that a look.

    Seguer on
Sign In or Register to comment.