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
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]);
	// }
}
}

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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]

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.