ScotDiddle Posted June 20, 2008 Share Posted June 20, 2008 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 More sharing options...
rhodesa Posted June 20, 2008 Share Posted June 20, 2008 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 https://forums.phpfreaks.com/topic/111095-why-doesnt-javascript-behave/#findComment-570141 Share on other sites More sharing options...
ScotDiddle Posted June 20, 2008 Author Share Posted June 20, 2008 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 https://forums.phpfreaks.com/topic/111095-why-doesnt-javascript-behave/#findComment-570159 Share on other sites More sharing options...
rhodesa Posted June 20, 2008 Share Posted June 20, 2008 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 https://forums.phpfreaks.com/topic/111095-why-doesnt-javascript-behave/#findComment-570171 Share on other sites More sharing options...
ScotDiddle Posted June 20, 2008 Author Share Posted June 20, 2008 rhodesa, Thanks... I am one step closer to being a Javascript Guru, like you. Scot Link to comment https://forums.phpfreaks.com/topic/111095-why-doesnt-javascript-behave/#findComment-570177 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.