Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checking for a Div

Status
Not open for further replies.

booeyOH

Programmer
May 30, 2007
48
US
Hello,
I newer to Ajax and have been using xajax. One problem I am having is that I try to execute some sort of action on a div, only to find out that it hasn't been "built" yet. So I get code that works sometimes and not others. Since you can't put <SCRIPT>runcodeatend();</SCRIPT> (because all applicable script pieces have been written), I was wondering if there was some way to check to see if a div exists.

Specifically, when the "page" loads, I want to show the contents of a certain div.

Please tell me if this makes no sense. I have a hard time framing the question.

Thanks,
Bryan
 
On the return javascript function after the ajax call, you can test for the existance of a div (assuming you know the id of the div you are testing for) like this:

Code:
if (document.getElementById("divId")) {
   alert("This div exists.");
}
else {
   alert("This div doesn't exist.");
   //Build the div most likely using document.createElement
}

[monkey][snake] <.
 
So theoretically I could do this:

while(!document.getElementById("divId"))
{
wait....
}

runFunctionOnDiv();

So the function wouldn't run until the div existed?
 
No - that would not really work, as your browser would probably lock up, or come up with a message saying that a script is taking too long to execute (tight loops in JS just don't cut the mustard).

You could use setInterval to create a timer that checks for the presence of the div every so often (say, every half a second or whatever). Check out some examples using Google.

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top