By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,607 Members | 1,931 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,607 IT Pros & Developers. It's quick & easy.

value to textbox question.

P: 28
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.

Expand|Select|Wrap|Line Numbers
  1. html
  2. <html>
  3.     <head>
  4.         <title></title> <script language = "javascript" type="text/javascript" src="rent.js">  </script> </head>
  5.     <body>
  6.         <script language = "javascript" type="text/javascript">
  7.             var rent =new Array();
  8.            rent[0] = new Rent("tenantA", 1000, "HouseA");
  9.             rent[1] = new Rent("tenantb", 2500, "HouseB");
  10.             rent[2] = new Rent ("tenantc", 6000, "Housec");
  11.  
  12.  
  13.         </script>
  14.         <form>
  15.            Tenant name : <input type="text" name="tenantTxt" />  <br/>
  16.            Rent Due : <input type="text" name="priceTxt" />  <br/>
  17.             Property : <input type = "text" name="propertyTxt" />
  18.             <br/>        
  19.  
  20.         <input type="button" name = "Tenant1" value="Tenant 1" onClick =" (document.forms[0][1].value=(rent[0].price)); "/>        
  21.             <input type="button" name = "Tenant2" value="Tenant 2" onClick =" (document.forms[0][1].value=(rent[1].price)); "/>        
  22.  
  23.  
  24.  
  25.         </form>
  26.     </body>
  27. </html>
  28.  
Expand|Select|Wrap|Line Numbers
  1. rent.js
  2.  function Rent(tenantName, price, property){
  3.      this.tenantName = tenantName;
  4.     this.price = price;
  5.     this.property = property;
  6.    this. toTextboxData =  toTextboxData;
  7.  }
  8.  function toTextboxData(){
  9.      tenantInfo = this.tenantName;
  10.      propertyPrice = this.price ;
  11.      propertyName =  this.property;
  12.           return tenantInfo + propertyPrice + propertyName
  13.  
  14.     }
  15.  
  16.  
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.
Feb 28 '11 #1

✓ answered by dgreenhouse

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

Expand|Select|Wrap|Line Numbers
  1. <html>
  2.   <head>
  3.     <title></title>  
  4.  
  5.      <script language = "javascript" type="text/javascript">
  6.        rent = new Array();
  7.        rent[0] = new Rent("tenantA", 1000, "HouseA");
  8.        rent[1] = new Rent("tenantB", 2500, "HouseB");
  9.        rent[2] = new Rent ("tenantC", 6000, "HouseC");
  10.  
  11.        function getTenantInfo(index) {
  12.          document.forms[0][0].value = rent[index].tenantName;
  13.          document.forms[0][1].value = rent[index].price;
  14.          document.forms[0][2].value = rent[index].property;
  15.          document.forms[0][3].value = rent[index].toTextboxData();
  16.        }
  17.  
  18.        function Rent(tenantName, price, property){
  19.          this.tenantName = tenantName;
  20.          this.price = price;
  21.          this.property = property;
  22.          this.toTextboxData =  toTextboxData;
  23.        }
  24.  
  25.        function toTextboxData(){
  26.          tenantInfo = this.tenantName;
  27.          propertyPrice = this.price ;
  28.          propertyName =  this.property;
  29.          return tenantInfo + ', ' + propertyPrice + ', ' + propertyName;
  30.        }       
  31.   </script>
  32. </head>
  33.   <body>
  34.     <form>
  35.       Tenant name : <input type="text" name="tenantTxt" />  <br/>
  36.       Rent Due : <input type="text" name="priceTxt" />  <br/>
  37.       Property : <input type="text" name="propertyTxt" /> <br/>
  38.       TextBox : <input type="text" name="toTextBox" />
  39.       <br/>        
  40.       <input type="button" name = "Tenant1" value="Tenant 1" onClick ="getTenantInfo(0)"/>        
  41.       <input type="button" name = "Tenant2" value="Tenant 2" onClick ="getTenantInfo(1)"/>
  42.       <input type="button" name = "Tenant3" value="Tenant 3" onClick ="getTenantInfo(2)"/>        
  43.      </form>
  44.   </body>
  45. </html>
  46.  

Share this Question
Share on Google+
2 Replies


dgreenhouse
Expert 100+
P: 250
Using your code this should work.
{Note: I included the code from rent.js (object definition) in the head.}

Expand|Select|Wrap|Line Numbers
  1. <html>
  2.   <head>
  3.     <title></title>  
  4.  
  5.      <script language = "javascript" type="text/javascript">
  6.        rent = new Array();
  7.        rent[0] = new Rent("tenantA", 1000, "HouseA");
  8.        rent[1] = new Rent("tenantB", 2500, "HouseB");
  9.        rent[2] = new Rent ("tenantC", 6000, "HouseC");
  10.  
  11.        function getTenantInfo(index) {
  12.          document.forms[0][0].value = rent[index].tenantName;
  13.          document.forms[0][1].value = rent[index].price;
  14.          document.forms[0][2].value = rent[index].property;
  15.          document.forms[0][3].value = rent[index].toTextboxData();
  16.        }
  17.  
  18.        function Rent(tenantName, price, property){
  19.          this.tenantName = tenantName;
  20.          this.price = price;
  21.          this.property = property;
  22.          this.toTextboxData =  toTextboxData;
  23.        }
  24.  
  25.        function toTextboxData(){
  26.          tenantInfo = this.tenantName;
  27.          propertyPrice = this.price ;
  28.          propertyName =  this.property;
  29.          return tenantInfo + ', ' + propertyPrice + ', ' + propertyName;
  30.        }       
  31.   </script>
  32. </head>
  33.   <body>
  34.     <form>
  35.       Tenant name : <input type="text" name="tenantTxt" />  <br/>
  36.       Rent Due : <input type="text" name="priceTxt" />  <br/>
  37.       Property : <input type="text" name="propertyTxt" /> <br/>
  38.       TextBox : <input type="text" name="toTextBox" />
  39.       <br/>        
  40.       <input type="button" name = "Tenant1" value="Tenant 1" onClick ="getTenantInfo(0)"/>        
  41.       <input type="button" name = "Tenant2" value="Tenant 2" onClick ="getTenantInfo(1)"/>
  42.       <input type="button" name = "Tenant3" value="Tenant 3" onClick ="getTenantInfo(2)"/>        
  43.      </form>
  44.   </body>
  45. </html>
  46.  
Mar 1 '11 #2

P: 28
Hi dgreenhouse,
Oh my goodness! Thank you so very much, Your a Genius!
Thank you so much for your understanding and prompt answer.
-sean
Mar 1 '11 #3

Post your reply

Sign in to post your reply or Sign up for a free account.