Solved

Object Required Error on document.getElementById

Posted on 2007-04-11
6
901 Views
Last Modified: 2012-08-13
I just want to ask why I'm getting object required error? is my incrementation not working?
Here are the codes:

<html>
<script>

var counter1 = 0;
var counter2 = 0;

function doValidate() {

      //alert("" + counter1);

      var componentFromInner1 = document.getElementById("innerSelect1" + counter1).value;
      //var componentFromInner2 = document.getElementById("innerSelect2" + counter2).value;

      if(componentFromInner1 != null && componentFromInner2 != null) {
            if (componentFromInner1 == componentFromInner2) {
                  alert("You can not select 2 similar components.");
            }
      }
}

function doAdd() {

      
      var str = '<table><tr><td><select id=\"innerSelect1" + counter1 + "\">';
      str += '<option name="option1">option1</option>';
      str += '<option name="option2">option2</option>';
      str += '<option name="option3">option3</option>';
      str += '</select>';
      str += '<select id=\"innerSelect2" + counter2 + "\">';
      str += '<option name="option1">option1</option>';
      str += '<option name="option2">option2</option>';
      str += '<option name="option3">option3</option>';
      str += '</select></td></tr></table>';

      document.getElementById('tb').innerHTML += str;
      counter1++;
      counter2++;
      
}
</script>
<body>
<table>
<tr>
<td>
<table>
<select name="select1">
<option name="option1">option1</option>
<option name="option2">option2</option>
<option name="option3">option3</option>
</select>
<select name="select2">
<option name="option1">option1</option>
<option name="option2">option2</option>
<option name="option3">option3</option>
</select>
</table>
<span id="tb"></span>
<button onclick="doAdd()">Add</button>
<button onclick="doValidate()">validate</button>
<script>
<td>
<tr>
</table>

</script>
</body>
</html>
0
Comment
Question by:simple21
6 Comments
 
LVL 22

Expert Comment

by:Ivo Stoykov
ID: 18888557
Hello simple21:

Here
>   var str = '<table><tr><td><select id=\"innerSelect1" + counter1 + "\">';

quotes seems unbalanced

try

var str = '<table><tr><td><select id="innerSelect1' + counter1 + '">';

HTH

!i!
0
 
LVL 10

Expert Comment

by:Jaax
ID: 18888701
The select objects are treated merely strings within a div and hence it is not added to the dynamic dom.
So javascript complains that the object not found

Anyway there are few errors too.
1.The <script>
<td>
<tr>
</table>
</script> shud be removed

2.><select id=\"innerSelect1" + counter1 + "\">';
shud be defined as ><select id="innerSelect'+counter1+'">';
Same applies for innerSelect2 too.

3.I tried replacing document.getElementById('tb').innerHTML += str; with document.writeln(str);
But still ran into problems
0
 
LVL 12

Expert Comment

by:Sinoj Sebastian
ID: 18888747
try my code

<html>
      <script>

var counter1 = 0;
var counter2 = 0;

function doValidate() {

      //alert("" + counter1);

      var componentFromInner1 = document.getElementById("innerSelect1" + counter1).value;
      var componentFromInner2 = document.getElementById("innerSelect2" + counter2).value;

      if(componentFromInner1 != null && componentFromInner2 != null) {
            if (componentFromInner1 == componentFromInner2) {
                  alert("You can not select 2 similar components.");
            }
      }
}

function doAdd() {

     
      counter1++;
      counter2++;
      var str = "<table><tr><td><select id='innerSelect1" + counter1 + "'>";
      str += "<option value=Option1>option1</option>";
      str += "<option value=Option2>option2</option>";
      str += "<option value=Option3>option3</option>";
      str += "</select>";
      str += "<select id='innerSelect2" + counter2 + "'>";
      str += "<option value=Option1>option1</option>";
      str += "<option value=Option2>option2</option>";
      str += "<option value=Option3>option3</option>";
      str += "</select></td></tr></table>";

      document.getElementById('tb').innerHTML += str;
     
}
      </script>
      <body>
            <table id="Table1">
                  <tr>
                        <td>
                              <table>
                                    <tr>
                                          <td><select id="innerSelect10" name="innerSelect10">
                                                      <option selected value=Option1>option1</option>
                                                      <option value=Option2>option2</option>
                                                      <option value=Option3>option3</option>
                                                </select>
                                                <select id="innerSelect20" name="innerSelect20">
                                                      <option selected value=Option1>option1</option>
                                                      <option value=Option2>option2</option>
                                                      <option value=Option3>option3</option>
                                                </select>
                                          </td>
                                    </tr>
                              </table>
                              <span id="tb"></span>
                              <INPUT id="Button1" type="button" value="Add" name="Button1" onclick="doAdd()"> <INPUT id="Button2" type="button" value="validate" name="Button2" onclick="doValidate()">
                        <td>
                  <tr>
                  </tr>
            </table>
      </body>
</html>


tell me if you have any problem
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 12

Expert Comment

by:Sinoj Sebastian
ID: 18888781
>I'm getting object required error?
You started with
    var counter1 = 0;
when you add new select id will be
    id="innerSelect10"
after that you incremented counter1
    counter1++;

then, when you press validate,
    var componentFromInner1 = document.getElementById(innerSelect11).value;
there is no element with id = innerSelect11, so the error.
0
 

Author Comment

by:simple21
ID: 18894858
It's working cool but it only validates the last one how about the other similar drop downs? It should be able to detect the old ones created and has similar value?
0
 
LVL 12

Accepted Solution

by:
Sinoj Sebastian earned 250 total points
ID: 18895733
What about this one?

<html>
      <script>            
            var counter = 0;

            function doValidate() {

                  for( var i=0;i<=counter;i++)
                  {
                        var componentFromInner1 = document.getElementById("innerSelect1" + i).value;
                        var componentFromInner2 = document.getElementById("innerSelect2" + i).value;

                        if(componentFromInner1 != null && componentFromInner2 != null) {
                                    if (componentFromInner1 == componentFromInner2) {
                                          alert("You can not select 2 similar components. Check row nomber " + (i+1));
                                          return;
                                    }
                        }
                  }
            }

            function doAdd() {
            
                  counter++;
                  var str = "<table><tr><td><select id='innerSelect1" + counter + "'>";
                  str += "<option value=Option1>option1</option>";
                  str += "<option value=Option2>option2</option>";
                  str += "<option value=Option3>option3</option>";
                  str += "</select>";
                  str += "<select id='innerSelect2" + counter + "'>";
                  str += "<option value=Option1>option1</option>";
                  str += "<option value=Option2>option2</option>";
                  str += "<option value=Option3>option3</option>";
                  str += "</select></td></tr></table>";

                  document.getElementById('tb').innerHTML += str;                  
            }            
      </script>
      <body>
            <form id=f1>
                  <table id="Table1">
                  <tr>
                        <td>
                              <table>
                                    <tr>
                                          <td><select id="innerSelect10" name="innerSelect10">
                                                      <option selected value=Option1>option1</option>
                                                      <option value=Option2>option2</option>
                                                      <option value=Option3>option3</option>
                                                </select>
                                                <select id="innerSelect20" name="innerSelect20">
                                                      <option selected value=Option1>option1</option>
                                                      <option value=Option2>option2</option>
                                                      <option value=Option3>option3</option>
                                                </select>
                                          </td>
                                    </tr>
                              </table>
                              <span id="tb"></span>
                              <INPUT id="Button1" type="button" value="Add" name="Button1" onclick="doAdd()">
                              <INPUT id="Button2" type="button" value="validate" name="Button2" onclick="doValidate()">
                        <td>
                  <tr>
                  </tr>
            </table>
      </body>
</html>
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

837 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question