/    Sign up×
Community /Pin to ProfileBookmark

Looping getElementById

I’m a javascript newer than newbie. I’m trying to write a script (for greasemonkey) that will continuously check the contents of a div for changes.

Thru a little searching and hours of trial and error I’ve got this to work…

[code]
divstate = (document.getElementById(‘state’).innerHTML);
alert(divstate);
[/code]

It works and will pop up the contents of the div with the ID state, once.

Everytime I try to stick it in a loop, it either pops up regardless of what’s in the div, or creates an endless loop that crashes my browser.

[code]
var endvalue = “OFF”

while (document.getElementById(‘state’).innerHTML != endvalue) {
divstate = (document.getElementById(‘state’).innerHTML);
if (divstate = endvalue) {
alert(divstate);
}
}
[/code]

I have tried a do while too but get the same results.

What am I doing wrong?

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@gil_davisOct 02.2009 — "Rookie" mistake.
if (divstate = endvalue) {[/quote]With that code, you have made them equal.
if (divstate == endvalue) {
A single equals is the assignment operator. A double equals is the compare operator.

However, it may still crash. Looping like that usually gets the browser stuck in the code and won't allow anything else to occur. It is better to use an event to trigger the compare. What changes the content of the DIV?
×

Success!

Help @crxvfr 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 5.7,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...