Solved

Object Required Error on document.getElementById

Posted on 2007-04-11
6
894 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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…

911 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now