how to get div elements present in a form using javascript?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nesha
    New Member
    • Sep 2010
    • 9

    how to get div elements present in a form using javascript?

    Hi,
    I have a html page in which im using a form. In that form i placed a div element within the form. That div element has another div element within that. how to get the innermost div element value in javascript? This is my html code.
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    <script type="text/javascript">
    document.onkeyup = KeyCheck;       
    function KeyCheck()
    {
       var obj = document.Form1.div1;
       alert(obj);
       for(var i=0;i<obj.length;i++) {
    	   alert(obj[i].name);
       }
    }
    </script>
    </head>
    <body>
    <form name="Form1">
    <div id="div1" style="height:500px; width:500px ;top: 250px; left: 250px; position: absolute; z-index: 2; visibility: show; border: #0000cc 1px solid">
    <div id="div2">
    <B>.</B>
    </div>
    </div>
    </form>
    </body>
    </html>
  • thesmithman
    New Member
    • Aug 2008
    • 37

    #2
    Hi Nesha,
    I'm a little unclear what you're trying to do. Presumably you will eventually add input elements to your form. Are you trying to use JavaScript to get the value of the inputs, after the user has submitted the form, for the purpose of form validation? If so, then you access input values something like this:

    Code:
    var inputValue = document.forms[0].yourInputName.value;
    where "yourInputN ame" is the name of the input.

    To answer your question literally as it is written, you would access the inner div with:

    Code:
    var div = document.getElementById('div2');
    but somehow I don't think that was what you meant.

    If you can provide a little more detail, we might be able to point you in the right direction.

    Happy coding!

    Comment

    • JKing
      Recognized Expert Top Contributor
      • Jun 2007
      • 1206

      #3
      The easiest way to get an element with an id would be the following:
      document.getEle mentById("div2" );

      Edit: Sorry for the duplicate answer. Slow to click the post button.

      Comment

      • nesha
        New Member
        • Sep 2010
        • 9

        #4
        Thanks for your reply. By using
        Code:
        var obj = document.getElementById("div2");
        i got the value. but my question is how to access the
        "div2" element value using form element?
        Im new to html. can u please explain me clearly.

        Comment

        • nesha
          New Member
          • Sep 2010
          • 9

          #5
          Thanks for your reply.
          now i got the value what i want. can u explain me why the following code is not working
          Code:
          var obj = document.Form1.div2;
          can you pls explain me?

          Comment

          • JKing
            Recognized Expert Top Contributor
            • Jun 2007
            • 1206

            #6
            Divs aren't added to the form's elements array. Therefore they cannot be accessed as if they were a property of the forms collection.

            Comment

            Working...