Reputation: 8366
I have a table which contain input types such as checkbox, text as below
var inputBoxes = table_check.getElementsByTagName('input'); //modified to get only input tags present within the table
for(var i=0; i<5; i++) {
Nos1=parseInt(inputBoxes[i+1].value);item_row_no=1;
if(inputBoxes[i].value){alert('No Input Box');} //fails at this if clause
else{Nos2=0;}
If there is less than 5 Input Types present inside the table it should throw alert.
Excepted result : No Input Box
from alert
Actual result : <nothing thrown>
if input type is less than 5;
The above script is working fine if there is exactly 5 input types inside the table.
Upvotes: 0
Views: 63
Reputation: 1098
inputBoxes
is an HTMLCollection.
Turn it into an array
like so:
var inputBoxes = document.getElementsByTagName('input');
inputBoxes = Array.prototype.slice.call(inputBoxes);
Then you can check if the inputBox is defined like so
if(inputBoxes[i] == undefined){
alert('No Input Box');
}
Upvotes: 1
Reputation: 42736
You should be using i<inputBoxes.length
as your for condition.
for(var i=0; i<inputBoxes.length; i++) {
Nos1=parseInt(inputBoxes[i+1].value);
item_row_no=1;
if(inputBoxes[i].value){
alert('No Input Box');
} else {
Nos2=0;
}
}
If for whatever reason you want to keep the i<5
condition you would need to check the existence of the array element and not the .value
, you should have been getting errors on the console because of the way your if was setup. Use the below:
if(!inputBoxes[i]){
console.log("Input Box "+ i +" Does Not Exist");
} else {
Nos2=0;
}
Upvotes: 1
Reputation: 865
Your question is not clear. If you just want to add a condition based on the number of input types, you can just use like given below.
var inputBoxes = table_check.getElementsByTagName('input'); //modified to get only input tags present within the table
if(inputBoxes.length<5){
alert('No Input Box');
}
Upvotes: 1