value to textbox question.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • seanh
    New Member
    • Sep 2007
    • 28

    value to textbox question.

    Hi,

    I have an array with some data(tenant, price, property) that I would like to place in a few text boxes of a form when someone clicks a button.

    I am only able to put one element of an array to the text box at a time like price or tenant or property but I would like to put all three data into their textbox when a button is clicked.

    ###ex. what I have###
    tenant name:
    rent due: price
    property:

    onclick show tenant(#)
    ############### ###############

    ###ex. what I would like###
    tenant name: tenantname
    rent due: price
    property: HouseA

    onclick show tenant(#)
    ############### ############

    So far if I put an alert box the code works I use the function toTextboxData() but its passing it to the textboxes that I have problems with.

    I would appreciate any help that any one could offer.

    Thank you in advance,
    -sean.

    Code:
    html
    <html>
        <head>
            <title></title> <script language = "javascript" type="text/javascript" src="rent.js">  </script> </head>
        <body>
            <script language = "javascript" type="text/javascript">
                var rent =new Array();
               rent[0] = new Rent("tenantA", 1000, "HouseA");
                rent[1] = new Rent("tenantb", 2500, "HouseB");
                rent[2] = new Rent ("tenantc", 6000, "Housec");
    			
    			
            </script>
            <form>
               Tenant name : <input type="text" name="tenantTxt" />  <br/>
               Rent Due : <input type="text" name="priceTxt" />  <br/>
                Property : <input type = "text" name="propertyTxt" />
                <br/>        
    			
    		<input type="button" name = "Tenant1" value="Tenant 1" onClick =" (document.forms[0][1].value=(rent[0].price)); "/>		
    			<input type="button" name = "Tenant2" value="Tenant 2" onClick =" (document.forms[0][1].value=(rent[1].price)); "/>		
    			
    	
    
            </form>
        </body>
    </html>
    Code:
    rent.js
     function Rent(tenantName, price, property){
     	this.tenantName = tenantName;
    	this.price = price;
    	this.property = property;
       this. toTextboxData =  toTextboxData;
     }
     function toTextboxData(){
     	tenantInfo = this.tenantName;
     	propertyPrice = this.price ;
     	propertyName =  this.property;
      		return tenantInfo + propertyPrice + propertyName
     	
    	}
    Is there a way to use toTextboxData() function to print to the text box or is there a better way of passing value to a form , the way I'm using the onclick doesn't seem to work. Any help would be greatly appreciated.

    Thank you.
  • dgreenhouse
    Recognized Expert Contributor
    • May 2008
    • 250

    #2
    Using your code this should work.
    {Note: I included the code from rent.js (object definition) in the head.}

    Code:
    <html>
      <head>
        <title></title>  
         
         <script language = "javascript" type="text/javascript">
           rent = new Array();
           rent[0] = new Rent("tenantA", 1000, "HouseA");
           rent[1] = new Rent("tenantB", 2500, "HouseB");
           rent[2] = new Rent ("tenantC", 6000, "HouseC");
     
           function getTenantInfo(index) {
             document.forms[0][0].value = rent[index].tenantName;
             document.forms[0][1].value = rent[index].price;
             document.forms[0][2].value = rent[index].property;
             document.forms[0][3].value = rent[index].toTextboxData();
           }
    
           function Rent(tenantName, price, property){
             this.tenantName = tenantName;
             this.price = price;
             this.property = property;
             this.toTextboxData =  toTextboxData;
           }
         
    	   function toTextboxData(){
             tenantInfo = this.tenantName;
             propertyPrice = this.price ;
             propertyName =  this.property;
             return tenantInfo + ', ' + propertyPrice + ', ' + propertyName;
           }	   
      </script>
    </head>
      <body>
        <form>
          Tenant name : <input type="text" name="tenantTxt" />  <br/>
          Rent Due : <input type="text" name="priceTxt" />  <br/>
          Property : <input type="text" name="propertyTxt" /> <br/>
          TextBox : <input type="text" name="toTextBox" />
          <br/>        
          <input type="button" name = "Tenant1" value="Tenant 1" onClick ="getTenantInfo(0)"/>        
          <input type="button" name = "Tenant2" value="Tenant 2" onClick ="getTenantInfo(1)"/>
          <input type="button" name = "Tenant3" value="Tenant 3" onClick ="getTenantInfo(2)"/>        
         </form>
      </body>
    </html>

    Comment

    • seanh
      New Member
      • Sep 2007
      • 28

      #3
      Hi dgreenhouse,
      Oh my goodness! Thank you so very much, Your a Genius!
      Thank you so much for your understanding and prompt answer.
      -sean

      Comment

      Working...