Note that there are some explanatory texts on larger screens.

plurals
  1. PONext/Previous button throws error
    primarykey
    data
    text
    <p>I keep getting the following error when I try to insert values by clicking the Next button on values that are already entered in.</p> <blockquote> <p>Unable to get the value of the property '0': object is null or undefined.</p> </blockquote> <p>I believe the error is happening at the last value in the array. I indicated the line below with a comment in the code. I want it to get the next value in the array but there isn't one created yet (it gets the next value just fine if the next value is not the last one in the array).</p> <p>I think that is the reason it's throwing an object <code>null</code>. However, I can't seem to check for the <code>null</code>/<code>undefined</code> and set it using statements such as <code>result[count+1][0] == undefined</code> because it doesn't work! It always throws an error no matter what I do.</p> <p>Some help would be much appreciated.</p> <p>Test case:</p> <ol> <li>Insert a value in text box 1 and text box 2</li> <li>Click Next</li> <li>Click Previous (in order to edit the values inserted above)</li> <li>Change the values in the text boxes to something else</li> <li>Click Next -- error happens</li> </ol> <p>Code:</p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;script type="text/javascript"&gt;&lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;script type="text/javascript"&gt; var result = new Array(); var count = 0; var input1 = new Array(); var input2 = new Array(); function move(direction) { if(direction == 'next') { var rate1 = [document.getElementById("txt1").value]; var rate2 = [document.getElementById("txt2").value]; if (result.length == count){ if (rate1 == '' || rate2 == '') { alert('you need to put in a value'); } else { result.push([[rate1], [rate2]]); document.getElementById("txt1").value = ''; document.getElementById("txt2").value = ''; count++; } } else { try{ (result[count][0]) = document.getElementById("txt1").value; (result[count][1]) = document.getElementById("txt2").value; document.getElementById("txt1").value = result[count++][0]; //error happening here. trying to show next value but there isn't one created yet. document.getElementById("txt2").value = result[count++][1]; document.getElementById("txt1").value = ''; document.getElementById("txt2").value = ''; } catch(err) { alert(err.description); } count++; } } if (direction == 'prev') { if(count &lt;= 0) { alert("no more elements"); } else { var prev_val1 = (result[count - 1][0]); document.getElementById("txt1").value = prev_val1; var prev_val2 = (result[count - 1][1]); document.getElementById("txt2").value = prev_val2; count--; } } document.getElementById("txtresult").value = result; } &lt;/script&gt; &lt;li&gt;text 1&lt;/li&gt; &lt;input type="text" id="txt1"/&gt; &lt;br&gt; &lt;li&gt;text 2&lt;/li&gt; &lt;input type="text" id="txt2"/&gt; &lt;br&gt; &lt;input type="button" id="btn" value="next" onclick="move('next')" /&gt; &lt;input type="button" id="btnprevious" value="previous" onclick="move('prev')" /&gt; &lt;br&gt; &lt;input type="text" id="txtresult"/&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload