473,407 Members | 2,676 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Get elements (div) name with javascript?

35
Okay I bet this is the easiest thing to figure out, but I've tried EVERYTHING to no avail. Maybe it's just not possible but I don't see why not.

I have a php page which is grabbing content from mysql and looping with while to display it. Each entry has a unique id, which I am setting as the name (the id is already taken in my script).

I then have this code:
Expand|Select|Wrap|Line Numbers
  1. window.onload = function() {
  2.     alert(document.getElementById('holder0').name);
  3. }
So I can see the name alerted to me. This yields undefined. 'holder0' exists and has a name.

Am I missing something? Why can't I get the elements name with getelbyid?

Thanks in advance.
Dec 20 '06 #1
5 40220
b1randon
171 Expert 100+
Okay I bet this is the easiest thing to figure out, but I've tried EVERYTHING to no avail. Maybe it's just not possible but I don't see why not.

I have a php page which is grabbing content from mysql and looping with while to display it. Each entry has a unique id, which I am setting as the name (the id is already taken in my script).

I then have this code:
Expand|Select|Wrap|Line Numbers
  1. window.onload = function() {
  2.     alert(document.getElementById('holder0').name);
  3. }
So I can see the name alerted to me. This yields undefined. 'holder0' exists and has a name.

Am I missing something? Why can't I get the elements name with getelbyid?

Thanks in advance.
Please post a snippet from your source with holder0.
Dec 20 '06 #2
The problem is that Divs don't normally have names. You would be able to read the "name" of a form element that way, I think.

But for any custom attribute, you need to use the getAttribute method, like this:
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <html>
  3. <head>
  4.     <title>Untitled</title>
  5.     <script type="text/javascript">
  6.     <!--
  7.         window.onload = function() {
  8.             alert(document.getElementById("div1").getAttribute("name"));
  9.         }
  10.     // -->
  11.     </script>
  12. </head>
  13.  
  14. <body>
  15.  
  16. <div id="div1" name="Betty">I am div 1</div>
  17.  
  18. </body>
  19. </html>
Dec 22 '06 #3
AricC
1,892 Expert 1GB
If ^^ doesn't help let us know what holder0 is
Dec 22 '06 #4
If you use getElementById you must have an id, if you only have a name use getElementByName.

hope this helped.
Apr 22 '09 #5
gits
5,390 Expert Mod 4TB
there is no standards-compliant method called:

Expand|Select|Wrap|Line Numbers
  1. document.getElementByName()
only:

Expand|Select|Wrap|Line Numbers
  1. document.getElementsByName()
note the s for Elements ... this method returns a collection of nodes with a name since just an id has to be unique within a document, but for names this is not required.

kind regards
Apr 22 '09 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

12
by: Jochen Daum | last post by:
Hi! I have a dynamically generated page (PHP), which contains an Explorer like view of items. I would like to hide multiple <tr>'s by name, but I can't figure out how thats done. I have this...
1
by: Invisible Man | last post by:
Thanks for help in advance... I'm trying to change the background of: <li class="noflyout" name="leftnav1"><a href="default.asp">Home </a></li> CSS set it to blue - but want to us JS to...
3
by: Andy | last post by:
Hi all, I'm writing a program which transforms an SGML document to XML, which I then pull elements out of. At a certain point, I have the following Xml snippit: <affliates> ...
6
by: Vikram | last post by:
I have added some input elements on a page using javascript at client side. when i submit the page, i am unable to access the values of input elements created using request.form. Are elements...
2
by: drew197 | last post by:
I am a newbie. This is someone else's code which I have to modify to work in Mozilla and Safari. The changes I made so far have allowed it to work in Mozilla but not Safari. It seems to be...
3
by: Bubu | last post by:
It's possible to hide or show e div from a iFrame page with javascript? If yes, please can anyone tell me how to do this? Thanks Bubu
1
by: JohnIdol | last post by:
Hi all, a quick one here. I am moving elements on the page clientside and I am going through strange issues. This one works: <html> <head> <title>Javascript Text</title>
3
by: Steve | last post by:
Hi; I'm working on a demo of using a timer on a web site that is made visible by making a div visible. My "PopIn Box" div is empty on the page. Before making it visible I used javascript to...
9
by: Gary Canada | last post by:
I have some code that works fine in IE, and chrome but does not work in Mozilla Firefox... here is a simple example: <div id='something'></div> <script language = "javascript" type =...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.