Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 19th, 2004, 03:20 AM
Authorized User
 
Join Date: Apr 2004
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!
 
Old December 19th, 2004, 06:05 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
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)
 
Old December 20th, 2004, 11:41 PM
Authorized User
 
Join Date: Apr 2004
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!

 
Old December 21st, 2004, 01:40 AM
Authorized User
 
Join Date: Apr 2004
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! =)

 
Old October 18th, 2006, 04:20 PM
Registered User
 
Join Date: Oct 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to co2tux2600
Default

Works great! Cheers!!

 
Old April 23rd, 2008, 01:53 AM
Registered User
 
Join Date: Mar 2006
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks a lot. It works like a charm






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





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