Solved

Object Required Error on document.getElementById

Posted on 2007-04-11
6
908 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

729 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