Get ‘undefined’ value when press ADD button in Javascript

Dynamically added row shows as ‘undefined’ when accessing

Page 1 of 1

3 Replies - 1530 Views - Last Post: 15 October 2007 - 07:45 AM

#1 squareCodeInaRoundApp  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 03-October 07

Get ‘undefined’ value when press ADD button in Javascript

Posted 11 October 2007 - 07:20 AM

I am trying to dynamically add a row but when I try to access it it shows as undefined. With the onload event I call a function (addRow) to add a specific number of rows. These I can access fine. It is when I try to add the row from an onclick event that I get the problem. See code snippets below...


function formatInit()
{
  for (var i = 0; i < 15; i++) 
  {
	addRow();
  }
  document.form.txtStoreNumber.focus();
}

function addRow()
{
  rowCntr++;
  //add a row to the rows collection and get a reference to newly added row
  var newRow = document.all("tblGrid").insertRow(rowCntr);
 
  //add 7 cells (between <td></td>) to new row and set innerHTML to contain text boxes
  var oCell = newRow.insertCell(0);
  oCell.innerHTML = "<input type='text' name='txtAssocName' style='width: 155px'>";

 //other 6 statement groups removed for readability
}

HTML**********
<body onload="formatInit();">
...
		<table id="tblGrid"  width="800px" style="table-layout:fixed">
		  <tr align="center" border="1" bgcolor="#000000" style>
			
			<td width="160px"><font color="#FFFFFF">Associate's Name</font></td>
			<!-- other 6 col headers lines removed for readability -->
		  </tr>
		</table>
		<br>
		<hr>
		  <b>click on Add Row button to add more rows above...</b>   
		<input type="button" value="Add Row" onclick="addRow();" />
		<hr>



Is This A Good Question/Topic? 0
  • +

Replies To: Get ‘undefined’ value when press ADD button in Javascript

#2 ahmad_511  Icon User is offline

  • MSX
  • member icon

Reputation: 131
  • View blog
  • Posts: 722
  • Joined: 28-April 07

Re: Get ‘undefined’ value when press ADD button in Javascript

Posted 14 October 2007 - 12:35 PM

Hi there
First of all I would ask: what is txtStoreNumber
Anyway I removed it.
And I think you have to declare the rowCntr before you use it that way
So declare it anywhere before the function addRow() like this:
var rowCntr;
function addRow()
{
}


If this didn't answer your question please add more details about the problem you're facing.

Regards
Was This Post Helpful? 0
  • +
  • -

#3 squareCodeInaRoundApp  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 03-October 07

Re: Get ‘undefined’ value when press ADD button in Javascript

Posted 15 October 2007 - 05:49 AM

View Postahmad_511, on 14 Oct, 2007 - 12:35 PM, said:

Hi there
First of all I would ask: what is txtStoreNumber
Anyway I removed it.
And I think you have to declare the rowCntr before you use it that way
So declare it anywhere before the function addRow() like this:
var rowCntr;
function addRow()
{
}


If this didn't answer your question please add more details about the problem you're facing.

Regards


Hello Ahmad,

You are correct in removing txtStoreNumber for it has no use in this problem (it has use when within all code). As for declaring "rowCntr", I have it declared as the very first line after the script tag.

Thank you so much for at least reviewing my problem.
Was This Post Helpful? 0
  • +
  • -

#4 ahmad_511  Icon User is offline

  • MSX
  • member icon

Reputation: 131
  • View blog
  • Posts: 722
  • Joined: 28-April 07

Re: Get ‘undefined’ value when press ADD button in Javascript

Posted 15 October 2007 - 07:45 AM

Welcome in Dream.In.Code
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1