Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Not sure why collection myCol is showing undefind when I try to run alert(myCol.count) 1

Status
Not open for further replies.

csphard

Programmer
Apr 5, 2002
194
US
Not sure why collection myCol is showing undefind when I try to run alert(myCol.count).

<html>
<head>
<title>testing testing </title>
</head>

<script type="text/javascript">

function Collection() {

var collection = {};

var order = [];



this.add = function(property, value) {

if (!this.exists(property)) {

collection[property] = value;

order.push(property);

}

}

this.remove = function(property) {

collection[property] = null;

var ii = order.length;

while (ii-- > 0) {

if (order[ii] == property) {

order[ii] = null;

break;

}

}

}

this.toString = function() {

var output = [];

for (var ii = 0; ii < order.length; ++ii) {

if (order[ii] != null) {

output.push(collection[order[ii]]);

}

}

return output;

}

this.getKeys = function() {

var keys = [];

for (var ii = 0; ii < order.length; ++ii) {

if (order[ii] != null) {

keys.push(order[ii]);

}

}

return keys;

}

this.update = function(property, value) {

if (value != null) {

collection[property] = value;

}

var ii = order.length;

while (ii-- > 0) {

if (order[ii] == property) {

order[ii] = null;

order.push(property);

break;

}

}

}

this.exists = function(property) {

return collection[property] != null;

}

}





function OnChange(dropdown)

{

var myindex = dropdown.selectedIndex

var SelValue = dropdown.options[myindex].value
console.log("testingtesting");
alert('my index is ' + myindex);

alert('my value is ' + SelValue);
var myCol=new Collection();
myCol.add("A",0);
myCol.add("B",1);
myCol.add("C",2);
alert(myCol.count);

alert("test");


}





</SCRIPT>

<body>


<form>
<table>
<tr><td><input type="radio" name="sex" value="male">Male</tr></td>
<tr>
<td>test o Date:</td>
<td><select name="oDateStr">
<option value="" selected="selected"></option>
<option value='06/16/2013'>06/16/2013</option>
<option value='01/01/2013'>01/01/2013</option></select>
</td>
<td>test a Date:</td>
<td>
<select name="aDateStr" onchange="OnChange(this.form.aDateStr)">
<option value="" selected="selected"></option>
<option value='07/01/2013'>07/01/2013</option>
<option value='06/19/2013'>06/19/2013</option>
<option value='06/18/2013'>06/18/2013</option>
<option value='01/10/2013'>01/10/2013</option>
<option value='01/01/2013'>01/01/2013</option>
</select>
</td>
</tr>


</table>

</form>
</body>
</html>

Howard
 
looks to me like the count property is the one that's undefined and not the myCol object
 
My goal is to be able create a collection client side.
I am looking for examples on creating a collection and reading it. I am dealing with dates. I want this collection to have 2 dates.
Then to be able to read through them and compare a date to them. On a match I want to read the second date.

Again I am looking for examples on creating and reading collections and arrays.


Howard
 
objects don't have a length property. and there is not a native 'count' property.

something like this may work.
Code:
<html>
    <head>
        <title>testing testing </title>
    </head>
    <script type="text/javascript">
        
        function Collection(){
            var collection = {};
            var order = [];
			
			this.count = function(){
				var keys = new Array;
				if (!Object.keys) {
				    Object.keys = function() {
				        var  k;
				        for (k in collection) {
				            if (Object.prototype.hasOwnProperty.call(collection, k)) {
				                keys.push(k);
				            }
				        }
				        return keys;
				    };
				} else {
					keys = Object.keys(collection);
				}
				return keys.length;
			}
            this.add = function(property, value){
                if (!this.exists(property)) {
                    collection[property] = value;
                    order.push(property);
                }
            }
            
            this.remove = function(property){
                collection[property] = null;
                var ii = order.length;
                
                while (ii-- > 0) {
                    if (order[ii] == property) {
                        order[ii] = null;
                        break;
                    }
                }
            }
            
            this.toString = function(){
                var output = [];
                for (var ii = 0; ii < order.length; ++ii) {
                    if (order[ii] != null) {
                        output.push(collection[order[ii]]);
                    }
                }
                return output;
            }
            
            this.getKeys = function(){
                var keys = [];
                for (var ii = 0; ii < order.length; ++ii) {
                    if (order[ii] != null) {
                        keys.push(order[ii]);
                    }
                }
                return keys;
            }
            
            this.update = function(property, value){
                if (value != null) {
                    collection[property] = value;
                }
                var ii = order.length;
                while (ii-- > 0) {
                    if (order[ii] == property) {
                        order[ii] = null;
                        order.push(property);
                        break;
                    }
                }
            }
            
            this.exists = function(property){
                return collection[property] != null;
            }
            
        }
        
        function OnChange(dropdown){
            var myindex = dropdown.selectedIndex
            var SelValue = dropdown.options[myindex].value
          //  console.log("testingtesting");
            alert('my index is ' + myindex);
            alert('my value is ' + SelValue);
            var myCol = new Collection();
            myCol.add("A", 0);
            myCol.add("B", 1);
            myCol.add("C", 2);
            alert(myCol.count());
            alert("test");
        }
    </script>
    <body>
        <form>
            <table>
                <tr>
                    <td>
                        <input type="radio" name="sex" value="male">Male
                    </td>
                </tr>
                <tr>
                    <td>
                        test o Date:
                    </td>
                    <td>
                        <select name="oDateStr">
                            <option value="" selected="selected"></option>
                            <option value='06/16/2013'>06/16/2013</option>
                            <option value='01/01/2013'>01/01/2013</option>
                        </select>
                    </td>
                    <td>
                        test a Date:
                    </td>
                    <td>
                        <select name="aDateStr" onchange="OnChange(this.form.aDateStr)">
                            <option value="" selected="selected"></option>
                            <option value='07/01/2013'>07/01/2013</option>
                            <option value='06/19/2013'>06/19/2013</option>
                            <option value='06/18/2013'>06/18/2013</option>
                            <option value='01/10/2013'>01/10/2013</option>
                            <option value='01/01/2013'>01/01/2013</option>
                        </select>
                    </td>
                </tr>
            </table>
        </form>
    </body>
</html>

or you could keep a counter

Code:
 function Collection(){
            var collection = {};
            var order = [];
			var count =0;
			
			this.add = function(property, value){
                if (!this.exists(property)) {
                    collection[property] = value;
                    order.push(property);
					count++;
                }
            }
            
            this.remove = function(property){
                collection[property] = null;
                var ii = order.length;
                
                while (ii-- > 0) {
                    if (order[ii] == property) {
                        order[ii] = null;
						count--;
                        break;
                    }
                }
            }
 
Thanks, I am looking at these examples now.

Howard
 
How do I get at the value of the collection... I want to check the value of the drop down with the value of the collection and on
am match read the second value it it exist. Some may have the second date some may not.

myCol.add("07/01/2013");
myCol.add("06/19/2013");
myCol.add("06/18/2013,06/16/2013");
myCol.add("01/10/2013");
myCol.add("01/01/2013,01/01/2013");

I change the myCol to include dates.



<html>
<head>
<title>testing testing </title>
</head>
<script type="text/javascript">

function Collection(){
var collection = {};
var order = [];

this.count = function(){
var keys = new Array;
if (!Object.keys) {
Object.keys = function() {
var k;
for (k in collection) {
if (Object.prototype.hasOwnProperty.call(collection, k)) {
keys.push(k);
}
}
return keys;
};
} else {
keys = Object.keys(collection);
}
return keys.length;
}
this.add = function(property, value){
if (!this.exists(property)) {
collection[property] = value;
order.push(property);
}
}

this.remove = function(property){
collection[property] = null;
var ii = order.length;

while (ii-- > 0) {
if (order[ii] == property) {
order[ii] = null;
break;
}
}
}

this.toString = function(){
var output = [];
for (var ii = 0; ii < order.length; ++ii) {
if (order[ii] != null) {
output.push(collection[order[ii]]);
}
}
return output;
}

this.getKeys = function(){
var keys = [];
for (var ii = 0; ii < order.length; ++ii) {
if (order[ii] != null) {
keys.push(order[ii]);
}
}
return keys;
}

this.update = function(property, value){
if (value != null) {
collection[property] = value;
}
var ii = order.length;
while (ii-- > 0) {
if (order[ii] == property) {
order[ii] = null;
order.push(property);
break;
}
}
}

this.exists = function(property){
return collection[property] != null;
}

}

function OnChange(dropdown){
var myindex = dropdown.selectedIndex
var SelValue = dropdown.options[myindex].value
// console.log("testingtesting");
alert('my index is ' + myindex);
alert('my value is ' + SelValue);

var myCol = new Collection();
myCol.add("07/01/2013");
myCol.add("06/19/2013");
myCol.add("06/18/2013,06/16/2013");
myCol.add("01/10/2013");
myCol.add("01/01/2013,01/01/2013");
alert(myCol.count());
alert("test");
}
</script>
<body>
<form>
<table>
<tr>
<td>
<input type="radio" name="sex" value="male">Male
</td>
</tr>
<tr>
<td>
test o Date:
</td>
<td>
<select name="oDateStr">
<option value="" selected="selected"></option>
<option value='06/16/2013'>06/16/2013</option>
<option value='01/01/2013'>01/01/2013</option>
</select>
</td>
<td>
test a Date:
</td>
<td>
<select name="aDateStr" onchange="OnChange(this.form.aDateStr)">
<option value="" selected="selected"></option>
<option value='07/01/2013'>07/01/2013</option>
<option value='06/19/2013'>06/19/2013</option>
<option value='06/18/2013'>06/18/2013</option>
<option value='01/10/2013'>01/10/2013</option>
<option value='01/01/2013'>01/01/2013</option>
</select>
</td>
</tr>
</table>
</form>
</body>
</html>
 
please post code within [ignore]
Code:
[/ignore] tags.

not sure what you are asking really, but perhaps something like this?

Code:
this.get = function (key){
				if(this.exists ( key )){
					return collection[key];
				}
				return false;
			}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top