Results 1 to 2 of 2

Thread: replace all textboxes with their respective values

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2004
    Posts
    121

    replace all textboxes with their respective values

    Hello all.

    I would like to loop through all textboxes on a page and replace each textbox with it's default value.

    So if I have:
    Code:
    <input name="user_name" type="text" value="solitario">
    I would like to remove the texbox itself, and write the value to the page, and here's the kicker: where the textbox appears.

    Right now I'm using a ridiculous set of stripos, strrpos, etc., functions. But I was wondering if this could be done with JavaScript.

    Many thanks.

  2. #2
    Hyperactive Member coothead's Avatar
    Join Date
    Oct 2007
    Location
    chertsey, a small town 25 miles south west of london, england.
    Posts
    285

    Re: replace all textboxes with their respective values

    Hi there solitario,

    have a look at this example it may suit your requirements...
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
       "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <title>remove inputs and replace with divs</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    
    <style type="text/css">
    .neat {
        width:6em;
        line-height:2em;
        border:0.25em double #999;
        font-family:serif;
        font-size:1em;
        color:#000;
        text-align:center;
        background-color:#eef;
        margin:0.25em auto;
     }
    </style>
    
    <script type="text/javascript">
    
       var dv=[];
       var count=0;
    
    window.onload=function(){
       obj=document.forms[0].getElementsByTagName('div')[0];
       cn=obj.childNodes;
    
    for(c=0;c<cn.length;c++) {
    if(cn[c].nodeType==1) {
       dv[count]=document.createElement('div');
       dv[count].className='neat';
       dv[count].appendChild(document.createTextNode(cn[c].value));
       count++;
      }
     }
    for(c=0;c<cn.length;c++) {
    if(cn[c].nodeType==1) {
       obj.removeChild(obj.childNodes[c]);
      }
     }
    for(c=0;c<dv.length;c++){
       obj.appendChild(dv[c]);
      }
     }
    </script>
    
    </head>
    <body>
    
    <form action="#">
    <div>
    <input name="user_name" type="text" value="solitario">
    <input name="user_name_one" type="text" value="lorem">
    <input name="user_name_two" type="text" value="ipsum">
    <input name="user_name_three" type="text" value="dolor">
    <input name="user_name_four" type="text" value="sit">
    </div>
    </form>
    
    </body>
    </html>
    coothead

Posting Permissions

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



Click Here to Expand Forum to Full Width