Trying to put the value of a variable into a function parameter

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Reply

Join Date: Feb 2007
Posts: 72
Reputation: adaykin is an unknown quantity at this point 
Solved Threads: 0
adaykin adaykin is offline Offline
Junior Poster in Training

Trying to put the value of a variable into a function parameter

 
0
  #1
May 26th, 2009
Hello I have a JSON array data.cells that I need to iterate through to get the values out of and put the values into an onclick event listener.

Right now the variable name data.cells[i].letter is being based through the buildString function for each cell in my board, instead of the value of the variable. Same goes for the x and y variables.

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. for(var i = 0; i < data.cells.length; i++) {
  2. var x = i % 4 + 1;
  3. var y = 0;
  4. if(i < 4) {
  5. y = 1;
  6. }
  7. else if(i < 8) {
  8. y = 2;
  9. }
  10. else if(i < 12) {
  11. y = 3;
  12. }
  13. else {
  14. y = 4;
  15. }
  16. document.getElementById('t' + i).addEventListener('click',function(data){
  17. buildString(this, data.cells[i].letter, x, y);}, false);
  18. document.getElementById('t' + i).setTextValue(data.cells[i].letter);
  19. }
My Website <-- check out my site!
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 1,402
Reputation: ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light 
Solved Threads: 224
Sponsor
ShawnCplus's Avatar
ShawnCplus ShawnCplus is offline Offline
Code Monkey

Re: Trying to put the value of a variable into a function parameter

 
0
  #2
May 26th, 2009
I have no idea what you're trying to do, you're already are passing those variables to the function.
  1. buildString(this, data.cells[i].letter, x, y);
GCS d- s+ a-->? C++(++++) UL+++ P+>+++ L+++ E--- W+++
N+ o K w++(---) O? !M- V PS+>++ PE+ Y+ PGP !t- 5? X- R tv+
b+>++ DI+ D G++>+++ e+ h+>++ r y+
PMs asking for help will not be answered, post on the forums. That's what they're there for.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 72
Reputation: adaykin is an unknown quantity at this point 
Solved Threads: 0
adaykin adaykin is offline Offline
Junior Poster in Training

Re: Trying to put the value of a variable into a function parameter

 
0
  #3
May 26th, 2009
So when the buildString function gets called instead of some value for data.cells[i].letter like 'A' the string data.cells[i].letter is being passed to the function. I want to get what the value of data.cells[i].letter is.
My Website <-- check out my site!
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 954
Reputation: essential will become famous soon enough essential will become famous soon enough 
Solved Threads: 131
Featured Poster
essential's Avatar
essential essential is offline Offline
Posting Shark

Re: Trying to put the value of a variable into a function parameter

 
0
  #4
May 28th, 2009
This might get what you need. An example of passing the arguments ( or parameter ) to another function.
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  3. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  4. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  5. <head>
  6. <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  7. <meta http-equiv="Content-Script-Type" content="text/javascript" />
  8. <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
  9. <title>Getting Javascript Variable and Display into Table Cell</title>
  10. <style type="text/css">
  11. /* <![CDATA[ */
  12. html, body {
  13. background-color : #ccc;
  14. color : #405060;
  15. font : normal normal normal 95%/1.4 "Trebuchet MS", "Bernard MT Condensed", Tahoma, Verdana, Helvetica, Arial, sans-serif;
  16. min-height : 600px;
  17. text-align : center;
  18. height : auto;
  19. width : auto; }
  20.  
  21. body .m-title {
  22. background-color : #444;
  23. border-top : 2px solid #000;
  24. border-bottom : 2px solid #000;
  25. color : #757575;
  26. margin-top : 0;
  27. padding : .100em 1em .100em 1em;
  28. text-align : left;
  29. width : auto; }
  30.  
  31. div#main {
  32. background-color : #f0f0f0;
  33. margin : 0 auto;
  34. height : inherit !important;
  35. padding : 0;
  36. width : 100%; }
  37.  
  38. div.tube {
  39. border : 1px solid;
  40. background-color : inherit;
  41. height : inherit !important;
  42. clear : both;
  43. padding : 1em;
  44. margin : 0;
  45. overflow : hidden; }
  46.  
  47. table {
  48. border-top : 1px solid;
  49. border-collapse;
  50. margin : 1em auto 0 auto;
  51. padding : 0;
  52. text-align : left;
  53. width : 100%; }
  54.  
  55. td {
  56. font : normal normal normal 10pt Verdana, Arial, sans-serif;
  57. border-bottom : 1px solid;
  58. letter-spacing : 2px;
  59. color : #696969;
  60. line-height : 1.5;
  61. white-space : pre-wrap;
  62. vertical-align : middle;
  63. padding : .300em 1em .300em 1em; }
  64.  
  65. /* ]]> */
  66. </style>
  67. <script type="text/javascript">
  68. // <![CDATA[
  69.  
  70. var getArgs;
  71. var buildString;
  72.  
  73. buildString = function( letter, cIndex, param3, param4 ) {
  74.  
  75. // I was assuming that the buildString function is implemented this way.
  76.  
  77. table = (( document.getElementById ) ? document.getElementById("data") : (( document.all ) ? document.all[ "data" ] : document.layers[ "data" ] )).rows[ 1 ].cells[ cIndex ];
  78.  
  79. text = "The letter <b>\u0022" + letter + "\u0022</b> is an argument ( or parameter ) passed by the getArgs() function gathered in first ROW at CELL " + (( cIndex ) + 1 ) + ".\n\n";
  80. text += "Varible <b>x</b> = <span style=\"color : #f50\">" + param3 + "</span>\n";
  81. text += "Varible <b>y</b> = <span style=\"color : #f50\">" + param4 + "</span>\n";
  82. table.innerHTML = text;
  83.  
  84. };
  85. getArgs = function( rIndex ) {
  86. data = (( document.getElementById ) ? document.getElementById("data") : (( document.all ) ? document.all[ "data" ] : document.layers[ "data" ] ));
  87. cLen = data.rows[ rIndex ].cells.length
  88. for ( var i = 0; i < cLen; i++ ) {
  89. x = i % 4 + 1;
  90. y = 0;
  91. y = (( i < 4 ) ? 1 : (( i < 8 ) ? y = 2 : (( i < 12 ) ? y = 3 : y = 4 )));
  92. buildString( data.rows[ rIndex ].cells[ i ].innerText, i, x, y );
  93. }
  94. };
  95. window.onload = function() { getArgs( 0 ) };
  96. // ]]>
  97. </script>
  98. </head>
  99. <body>
  100. <div id="main">
  101. <div id="content" class="tube">
  102.  
  103. <h2 class="m-title">Getting Javascript Variables</h2>
  104. <table id="data" frame="void" rules="none" summary="Getting Javascript Variables and passing it into Table Cell.">
  105. <tr>
  106. <td>A</td>
  107. <td>B</td>
  108. <td>C</td>
  109. </tr>
  110. <tr>
  111. <td>Not Set</td>
  112. <td>Not Set</td>
  113. <td>Not Set</td>
  114. </tr>
  115. </table>
  116. </div>
  117. </div>
  118. </body>
  119. </html>
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC