TypeError: .val is not a function

I have the following code in which I select all matching elements starting with the same name, with the exception of one that I do not want to include in the group.

var myInputBoxes= $('input[id ^= "SubjectText"]').not('#SubjectTextNew'); for (i = 0 ; i < myInputBoxes.length; i++){ var SubjectId = myInputBoxes[i].id.replace('SubjectText', ''); var Subject = myInputBoxes[i].val(); } 

This gives me the following error in firefox

TypeError: myInputBoxes [i] .val is not a function

Why doesn't it work in val function?

+6
source share
2 answers

Accessing a jQuery object using bracket notation returns a DOMElement that does not have a val() function. If you want to get an element by its index in a consistent set, you need to use eq() :

 var Subject = myInputBoxes.eq(i).val(); 

Alternatively, you can save the DOMElement and use the value property:

 var Subject = myInputBoxes[i].value; 
+23
source

Since subjectBoxes[i] not a jQuery object, if it is a jQuery object, then you can use .eq () to get the jQuery wrapper reference to the element in the passed index

 var myInputBoxes = $('input[id ^= "SubjectText"]').not('#SubjectTextNew'); myInputBoxes.each(function (e, el) { var SubjectId = this.id.replace('SubjectText', ''); var Subject = subjectBoxes.eq(i).val(); }) 
+1
source

Source: https://habr.com/ru/post/985871/


All Articles