/    Sign up×
Community /Pin to ProfileBookmark

Object that dont exist…

var currSchema = null;
function hilightIt() {
var q, len = schema.getListsLength();
for(q=0; q<len; ++q) document.getElementById(“s”+q).className = “”;
currSchema = this;
currSchema.className = “hlt”;
return true;
}
window.onload = function () {
var q, len = schema.getListsLength();
for(q=0; q<len; ++q) document.getElementById(“s”+q).onclick = hilightIt;
return true;
}

I use these functions to highligt some <TD>´s onclick, but the page is dynamic- so sometimes the <TD>´s have been removed- how do I make the scripts check if they exist? (this goes for onload function as well- at onload- some of <TD>´s have not yet been added to the page).

to post a comment
JavaScript

13 Comments(s)

Copy linkTweet thisAlerts:
@phpnoviceApr 11.2006 — This:
<i>
</i>window.onload = function () {
var q, len = schema.getListsLength();
for(q=0; q&lt;len; ++q) document.getElementById("s"+q).onclick = hilightIt;
return true;
}

would be changed to this:
<i>
</i>window.onload = function () {
var q, obj, len = schema.getListsLength();
for(q=0; q&lt;len; ++q) {
if(obj=document.getElementById("s"+q)) {
obj.onclick = hilightIt;
}
}
return true;
}

Note: Do not change the single equal sign in the [b]if[/b] statement above. This is done on purpose, is intentional, is correct, does what it is supposed to do, and works correctly as shown! ?
Copy linkTweet thisAlerts:
@TarantinoauthorApr 11.2006 — tnx a lot:-)

I always wondered what is the difference from using == and =?
Copy linkTweet thisAlerts:
@TarantinoauthorApr 11.2006 — This worked fine- but in another function I need it to do a code if 2 statements are true.

1 a must be 0

and

the cell with ID="c0" may not have been added to the page yet. Why is this not working. Its executing the code no matter if s0 is added:

if (a==0 && obj!=document.getElementById('s0'))
{
mycode
}
Copy linkTweet thisAlerts:
@TarantinoauthorApr 11.2006 — ahh- ill save u time and answer it myself:

if (a==0 && document.getElementById('s0')==null)
{code}
Copy linkTweet thisAlerts:
@KorApr 11.2006 — tnx a lot:-)

I always wondered what is the difference from using == and =?[/QUOTE]


the operators == and === do a bitwise [B]comparision[/B] (=== is a strict one), while = is a simple [B]asignment[/B] operator (gives to the left term the value of the right term)

var myVar = 'somevalue';// this is an asignment

myVar == 'somevalue'; // this is a comparision. It will return Booleans [B]true[/B] or f[B]alse[/B] values as evaluation.

Ex:

var myVar = somevalue';

alert(myVar == 'somevalue')
Copy linkTweet thisAlerts:
@phpnoviceApr 11.2006 — ahh- ill save u time and answer it myself:[/QUOTE]
I would have coded this:

if (a==0 && document.getElementById('s0')==null)

as this:

if (a==0 && document.getElementById('s0'))

or this:

if (document.getElementById('s0') && a==0)
Copy linkTweet thisAlerts:
@TarantinoauthorApr 11.2006 — I would have coded this:

if (a==0 && document.getElementById('s0')==null)

as this:

if (a==0 && document.getElementById('s0'))

or this:

if (document.getElementById('s0') && a==0)[/QUOTE]


I actually did try that first. For some reason its not working for me- I just tried it again now. It dont execute the code even though both conditions should be true. My command works. Odd...
Copy linkTweet thisAlerts:
@balloonbuffoonApr 12.2006 — 
I would have coded this:

if (a==0 && document.getElementById('s0')==null)

as this:

if (a==0 && document.getElementById('s0'))

or this:

if (document.getElementById('s0') && a==0)[/QUOTE]

"document.getElementById('s0')" is not the same as "document.getElementById('s0')==null". You'd have to do "!document.getElementById('s0')", right?

--Steve
Copy linkTweet thisAlerts:
@phpnoviceApr 12.2006 — "document.getElementById('s0')" is not the same as "document.getElementById('s0')==null".[/QUOTE]
Oops, an oversight on my part. ? Hey, nobody's perfect! ? And, anybody who says they are is a liar. :p
Copy linkTweet thisAlerts:
@balloonbuffoonApr 12.2006 — Yea, thats true! But its just that you're such a javascript expert, I thought I was going crazy!

--Steve
Copy linkTweet thisAlerts:
@phpnoviceApr 12.2006 — Yea, thats true! But its just that you're such a javascript expert, I thought I was going crazy![/QUOTE]
[I]"There's many a slip between the cup and the lip."[/I]

I only scored in the 98th percentile, against over 200,000 other coders, at BrainBench.com a few years ago. ?
Copy linkTweet thisAlerts:
@balloonbuffoonApr 12.2006 — Ok, so you proved me wrong, you're not an expert. Any expert would've scored in the 99th percentile. :p

--Steve
Copy linkTweet thisAlerts:
@phpnoviceApr 12.2006 — Ok, so you proved me wrong, you're not an expert.[/QUOTE]
Nope, I'm not an ex-spurt -- which is an out-of-work drip under pressure. I, on the other hand, have a job. ?
×

Success!

Help @Tarantino spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...