Jump to content

Why Doesn't javascript behave...


ScotDiddle

Recommended Posts

Just call me a javascript Noob...

 

While trying to enhance a vendor supplied js file, I needed to find out the name of the element their system was generating, so I wrote the following...

 


    function determineVisibility() {

    if(!document.body||!document.body.innerHTML) {
	    return;
    }

    for (key in this.TC0p) {

	    // alert(key); // Turn on to see each property name

	    if (key == 'id') {
		    alert('key: ' + key + '        haystack : ' +  this.TC0p.id);
	    }

    }

    }

 

If I turn on 'alert(key)'  I see the name of each property, but if I try to see the value of the property via:

 

alert('key: ' + key + '        haystack : ' +  this.TC0p  + '.' + key);

 

 

I get back  '[object].keyname' ( no value associated with alert )

 

After I know the names of the properties, via the simple 'alert(key)', then hard code it like I did in my example above for 'id',

I get the value.

 

Is what I want to do possible ( intercept the name of the property, and in the same loop display it's value...) ?

 

Am I on the right track, and I just don't have the syntax correct ?

 

I hate javascript. (Even though I find it quite useful  :) )

 

Thanks for any and all help.

 

Scot L. Diddle, Richmond VA

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/111095-why-doesnt-javascript-behave/
Share on other sites

are you trying to do this?

 

function determineVisibility() {
if(!document.body||!document.body.innerHTML) {
	return;
}
for (key in this.TC0p) {
	// alert(key); // Turn on to see each property name
	// if (key == 'id') {
	alert('key: ' + key + '	value : ' +  this.TC0p[key]);
	// }
}
}

rhodesa,

 

You Rock !

 

My original question remains... Why doesn't Javascript behave...

 

After posting my original query, I found the link on this board about best js coding practices... I read it and saw the write-up about using brackets and changed my code to use brackets.  Didn't work.  I got [object][id].

 

I tried:

 

alert('key: ' + key + '        haystack : ' +  this.TC0p + '[' + key + ']');

 

When I cut and pasted your example, it worked as advertised. 

 

Why does yours work and mine doesn't ? ( hypothetical question -- No need to answer  ( unless you know  :) ) ).

 

Thanks so much.

 

Scot L. Diddle, Richmond VA

 

Because you are printing the brackets as a string, instead of using them to access the part of an object

s = string, v = variable

alert('key: ' + key + '        haystack : ' +  this.TC0p + '[' + key + ']');

      |  s  |  v  |          s            |      v    |  s  |  v  | s |

get it?

this.TC0p + '[' + key + ']'

should be just

this.TC0p[key]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.