0

When I try the following:

<html>
    <body>
        <script type="text/javascript">
            window.onload = function() {
                var test1 = document.getElementsByName("div1");
                alert(test1[0]);
                var test2 = test1[0].getElementsByName("div2");
                alert(test2[0]);
            }
        </script>
        <div name="div1">
            <div name="div2">
            </div>
        </div>
    </body>
</html>

It doesn't work the way I intend it to. I made a form and I need to be able to get the form data in a similar manner to what I was testing with this.

3
  • 2
    Damn! my mind reading machine has a bug, ok, I give up, what was your intended way to use it? Commented Jul 7, 2011 at 3:16
  • can you use jquery? and yes what are you trying to do? Commented Jul 7, 2011 at 3:17
  • 1
    @Asaph: developer.mozilla.org/En/DOM/Document.getElementsByName Commented Jul 7, 2011 at 3:19

3 Answers 3

3

getElementsByName should only be called from a document element:

var test2 = document.getElementsByName('div2');

What's more, you really should only use the name attribute for form elements, not divs.

If you want an API that more-easily lets you do searches within a DOM element, consider using jQuery:

<html>
  <head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
    <script>
      $(function() {
          var div1 = $('#div1');
          var div2 = div1.find('#div2');
      });
    </script>
  </head>
  <body>
    <div id="div1">
      <div id="div2"></div>
    </div>
  </body>
</html>

Of course, since ids must be unique document-wide, there's usually no reason to search within another element for a specific ID. Therefore, you don't really need anything fancy like jQuery for something that simple.

Sign up to request clarification or add additional context in comments.

3 Comments

If you are using simple ids jQuery is overkill. div1 = document.getElementById("div1"); div2 = document.getElementById("div2"); You don't even need to use div1 as the context because ids are presumed to be unique.
I totally agree. Added that as note to the answer.
Thanks. I'm not looking to get specific elements by ID though. I have a form with a variable amount of options and sub-options. I need to get them in the exact order they appear. How else can I do that?
1

If I recall correctly, only the document (HTMLDocument) has the getElementsByName method. DOM Elements do not have that method and you cannot apply it by using them as the context.

3 Comments

+1, but the name attribute is valid for many more elements than form (there are 14 in total), such as all the form controls, img, a, object and so on.
@RobG: Is your comment supposed to be directed towards one of the other answers?
Ooops! Should have been a comment on the Jacob's answer. :-(
0

There is no name in div's attributes. Why not use id instead?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.