1

The problem is simple. When I take the variable into the array it doesn't work. When I write the variable into inputs it works. How can I pass the variable into the array?

var testW = document.querySelectorAll("input[type='text']");
var testQ = document.getElementsByClassName("deleteObjectbyJS");

var testlength = testW.length;

if(testW[testlength].value == ""){
    testQ[0].style.display = "none";
}
1
  • 1
    JavaScript arrays are 0-based indexed. If your array has no items in it, the length of the array is 0 (testlength). When your code tries to access the data at index 0 of the array, it finds there's nothing there which results in the array returning undefined. Commented Aug 18, 2017 at 15:25

3 Answers 3

4

Arrays in JS start indexing from 0 to SIZE-1 so your code is wrong here:

if(testW[testlength-1].value == ""){
    testQ[0].style.display = "none";
}

I don't know if this solve your problem completly but this is an error for sure

Sign up to request clarification or add additional context in comments.

Comments

0

testlength is length of the list. When you trying to access testW[testlength] you actually access non existing element after the last element because elements indexes are between 0 and testlength-1.

Comments

0

testW[testlength] should be testW[testlength-1]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.