Two questions:
1. I created Javascript objects called oNewObj using {} construct as
in the follwoing code segment. When I clicked on the <td> element, I
got runtime error "oNewObj is undefined". When I changed the <td>
onclick event to doThis(this), then I can exam (this) object.
However, I really want to exam the oNewObj. Does anyone know how to
do it? Thanks.
2. I need to add additional things to the oNewObj, how could I append
new object to it so that it would look like: {things:[{code:100,
color:'green'}],[{key:215, color:'red'}]};
<html>
<body>
<script language='javas cript'>
<!--
var oNewObj = {things:[{code:1, color:'abc'}]};
var strHTML;
strHTML = "<table><tr >"
strHTML += "<td id='1' onclick='javasc ript:doThis(oNe wObj)'>click
this</td>"
strHTML += "</tr>"
strHTML += "<tr>"
strHTML += "<td id='2' onclick='javasc ript:doAll(oNew Obj)'></td>"
strHTML += "</tr></table>"
DIVresult.inner HTML = strHTML;
function doThis(o)
{
alert(o.things[this.id].code + '\n' + o.things[this.id].color);
}
function doAll()
{
var i;
for (i=0; i<=oNewObj.thin gs.length; i++)
alert(oNewObj.t hings[i].code + '\n' + oNewObj.things[i].color);
}
//-->
<DIV id='DIVresult'> </DIV>
</body>
</html>
1. I created Javascript objects called oNewObj using {} construct as
in the follwoing code segment. When I clicked on the <td> element, I
got runtime error "oNewObj is undefined". When I changed the <td>
onclick event to doThis(this), then I can exam (this) object.
However, I really want to exam the oNewObj. Does anyone know how to
do it? Thanks.
2. I need to add additional things to the oNewObj, how could I append
new object to it so that it would look like: {things:[{code:100,
color:'green'}],[{key:215, color:'red'}]};
<html>
<body>
<script language='javas cript'>
<!--
var oNewObj = {things:[{code:1, color:'abc'}]};
var strHTML;
strHTML = "<table><tr >"
strHTML += "<td id='1' onclick='javasc ript:doThis(oNe wObj)'>click
this</td>"
strHTML += "</tr>"
strHTML += "<tr>"
strHTML += "<td id='2' onclick='javasc ript:doAll(oNew Obj)'></td>"
strHTML += "</tr></table>"
DIVresult.inner HTML = strHTML;
function doThis(o)
{
alert(o.things[this.id].code + '\n' + o.things[this.id].color);
}
function doAll()
{
var i;
for (i=0; i<=oNewObj.thin gs.length; i++)
alert(oNewObj.t hings[i].code + '\n' + oNewObj.things[i].color);
}
//-->
<DIV id='DIVresult'> </DIV>
</body>
</html>
Comment