Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-18-2012, 06:30 AM   PM User | #1
Kuan
New to the CF scene

 
Join Date: Feb 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Kuan is an unknown quantity at this point
Error: this.basket[index].setCondition is not a function

Hi there,

I have designed text fields which allow user to enter the with single/multi rows
data into database. I have 2 javascript files which are written in the same way, just different variable and function name. But one of them is not working.

Below is my scripts:-

Code:
/**
 * Broswer class.
 *
 */
function Browser(){
  this.dom  = document.getElementById?1:0;
  this.ie4  = (document.all && !this.dom)?1:0;
  this.ns4  = (document.layers && !this.dom)?1:0;
  this.ns6  = (this.dom && !document.all)?1:0;
  this.ie5  = (this.dom && document.all)?1:0;
  this.ok   = this.dom || this.ie4 || this.ns4;
  this.platform = navigator.platform;
}
var browser = new Browser();

/**
 * advSearchTable Class
 * 
 */
function detailTable(nameCond)
{
  this.nameCond   = nameCond;
  this.basket = new Array();
  this.segObj = document.getElementById('detailTable');
}

detailTable.prototype.select = function(conditionSelected)
{
	var i=this.basket.length; 
	this.basket[i]=new Segment(conditionSelected);
	this.renderDetailTable();
}

detailTable.prototype.remove = function(index)
{
  this.basket[index].deleteSegment();
  this.renderDetailTable();
}

detailTable.prototype.changeValue = function(index, field, value)
{
  this.basket[index].setCondition(value);
  this.renderDetailTable();
}
detailTable.prototype.renderDetailTable = function()
{
  var unit        = 0;
  var counter     = 0;
  var tableNumber = 1;
  var no          = 1;
  var segTable    = "";
  
  segTable += "<table>";
  
  for(i=0; i<this.basket.length; i++) {
    var seg=this.basket[i];
		if(seg.active=="No")
			continue;
		unit++;
    
    var conditionSelected = seg.conditionSelected
    var condition         = "condition:"+counter;
    
    if (no%2==0) segTable += "<tr>"; else segTable += "<tr class='trAlt2'>";
    segTable += "<td>"+no+".</td>";
    segTable += "<td nowrap width='90%'>";
    segTable += '<textarea name="'+condition+'" id="'+condition+'" style="height:40px;" size = "95" maxlength = "95" onChange="detailTable.changeValue('+i+',\'condition\',this.value);">'+conditionSelected+'</textarea></td>';
    segTable += "<td><input type=\"button\" name=\"Remove\" value=\"X\" onClick=\"detailTable.remove('"+i+"', false);\" class=\"remove\"></td>";
    segTable += '</tr>';
    segTable += ''
    tableNumber++;
    counter++;
    no++;
  }
  segTable += "</table><br>";  
  $("#hidRowCount").val(counter);

  this.segObj.innerHTML=((unit>0)?segTable:"");
}

function Segment(conditionSelected)
{
	this.active            = "Yes";
  this.conditionSelected = conditionSelected;
}

Segment.prototype.deleteSegment = function()
{
	this.active = "No";
}

Segment.prototype.setCondition = function(value)
{
	this.conditionSelected = value;
}
Error message is shown in the Title. The error pointed to the red highlighted in the line above.
Please advise.

Thanks
Kuan is offline   Reply With Quote
Old 12-18-2012, 07:19 AM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Code:
this.ie4  = (document.all && !this.dom)?1:0;
  this.ns4  = (document.layers && !this.dom)?1:0;
  this.ns6  = (this.dom && !document.all)?1:0;
  this.ie5  = (this.dom && document.all)?1:0;
Those browsers have been one with Nineveh and Tyre for very many years.

It is your responsibility to die() if necessary….. - PHP Manual
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Old 12-18-2012, 07:37 PM   PM User | #3
Logic Ali
Regular Coder

 
Logic Ali's Avatar
 
Join Date: Sep 2010
Location: London
Posts: 961
Thanks: 0
Thanked 198 Times in 193 Posts
Logic Ali will become famous soon enoughLogic Ali will become famous soon enough
Then do something to find out why:
Code:
detailTable.prototype.changeValue = function(index, field, value)
{
  alert( typeof this.basket[index].setCondition  );
  this.basket[index].setCondition(value);
  this.renderDetailTable();
}
If you're writing object-orientated code, don't ruin it by creating global variables:

Code:
 for( var i=0; i<this.basket.length; i++) {
Logic Ali is offline   Reply With Quote
Old 12-19-2012, 05:41 AM   PM User | #4
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,238
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Quote:
Originally Posted by Philip M View Post
Code:
this.ie4  = (document.all && !this.dom)?1:0;
  this.ns4  = (document.layers && !this.dom)?1:0;
  this.ns6  = (this.dom && !document.all)?1:0;
  this.ie5  = (this.dom && document.all)?1:0;
Those browsers have been one with Nineveh and Tyre for very many years.
On top of that, it's not even accurate.

FireFox, Safari, and Chrome (I think) will all show up as ns6.

It's hard to imagine any more pointless code in today's market. Or, for that matter, at any time since about 2001 or so.

The good part is that, after shoving in that junk code, it does not appear that he actually uses it any place.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is online now   Reply With Quote
Old 12-19-2012, 05:46 AM   PM User | #5
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,238
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Quote:
Originally Posted by Logic Ali View Post
Then do something to find out why:
Code:
detailTable.prototype.changeValue = function(index, field, value)
{
  alert( typeof this.basket[index].setCondition  );
  this.basket[index].setCondition(value);
  this.renderDetailTable();
}
Ummm...I will bet that will just alert null.

Possibly better:
Code:
detailTable.prototype.changeValue = function(index, field, value)
{
    alert( "this: " + typeof this );
    alert( "this.basket: " + typeof this.basket );
    alert( "index: " + index + " vs. this.basket.length: " + this.basket.length );
    alert( "this.basket[index]: " + typeof this.basket[index] );
    ...
}
Though of course much better than all that gobbledy-gook would be to simply use a debugger and inspect the elements to see what's what.

alert( typeof this.basket[index].setCondition );
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is online now   Reply With Quote
Reply

Bookmarks

Tags
javascript, javascript function, javascript method, this.basket method

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:54 PM.


Advertisement
Log in to turn off these ads.