Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old December 19th, 2004, 03:20 AM
Authorized User
 
Join Date: Apr 2004
Location: , , .
Posts: 63
Thanks: 0
Thanked 0 Times in 0 Posts
Default Check if Div Layer/Object Exists

Hi! Can someone please tell me how do I check if a div layer or an object exists in a page? Please help...

Thanks!
Reply With Quote
  #2 (permalink)  
Old December 19th, 2004, 06:05 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

If it has a known id then it's simple in modern browsers:
Code:
function testForObject(Id, Tag)
{
  var o = document.getElementById(Id);
  if (o)
  {
    if (Tag)
    {
      if (o.tagName.toLowerCase() == Tag.toLowerCase())
      {
        return o;
      }
    }
    else
    {
      return o;
    }
  }
  return null;
}
Then you can test for any element with an id of "divMain" with:
Code:
var o = testForObject("divMain");
if (o)
{
  //do something will object
}
else
{
  alert("No object found.");
}
or you can limit it to objects of a specific type:
Code:
var o = testForObject("divMain", "div");
if (o)
{
  //do something will object
}
else
{
  alert("No object found with specific tag anme.");
}

--

Joe (Microsoft MVP - XML)
Reply With Quote
  #3 (permalink)  
Old December 20th, 2004, 11:41 PM
Authorized User
 
Join Date: Apr 2004
Location: , , .
Posts: 63
Thanks: 0
Thanked 0 Times in 0 Posts
Default

i tried using the code you've given me..i came up with this...

<html>
<head>
<title>Untitled Document</title>

<!--
<script>
function testForObject(Id)
{
  var o = document.getElementById(Id);
  if (o)
  {

 alert("ok!");

  }
  alert("No object found.");
}
</SCRIPT>
-->

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body bgcolor="#FFFFFF" >


<DIV ID="oDiv1">Div #1</DIV>
<DIV ID="oDiv2">Div #2</DIV>
<DIV ID="oDiv3">Div #3</DIV>
<INPUT TYPE="button" VALUE="Get Names" onclick="testForObject('oDiv1');">

</body>
</html>


i keep getting the error 'object expected'. i don't know why. help!

Reply With Quote
  #4 (permalink)  
Old December 21st, 2004, 01:40 AM
Authorized User
 
Join Date: Apr 2004
Location: , , .
Posts: 63
Thanks: 0
Thanked 0 Times in 0 Posts
Default

i found the error in my code..

<!--
<script>
function testForObject(Id)
{
  var o = document.getElementById(Id);
  if (o)
  {

 alert("ok!");

  }
  alert("No object found.");
}
</SCRIPT>
-->

the tags should be between the <script> tags...hehehe! thanks for your help joe! =)

Reply With Quote
  #5 (permalink)  
Old October 18th, 2006, 04:20 PM
Registered User
 
Join Date: Oct 2006
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to co2tux2600
Default

Works great! Cheers!!

Reply With Quote
  #6 (permalink)  
Old April 23rd, 2008, 01:53 AM
Registered User
 
Join Date: Mar 2006
Location: Kolkata, West Bengal, India.
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks a lot. It works like a charm

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
check whether COM object exists EddyT C# 0 June 15th, 2007 04:10 AM
Check if Object Exists in the Database Coby Access VBA 2 June 7th, 2007 02:56 PM
check whether a particular session exists or not chayanvinayak PHP How-To 1 May 1st, 2006 04:09 PM
check first to see if the file exists crmpicco Classic ASP Professional 2 December 1st, 2005 12:34 PM
How to check if connection already exists Ciarano VB How-To 3 March 9th, 2004 10:24 AM



All times are GMT -4. The time now is 08:25 AM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.