Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: .val is not a function

Tags:

I have the following code, where I am selecting all matching elements that start with the same name, excluding one I do not want included 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 would it fail on the val function?

like image 352
Neil P Avatar asked Apr 24 '15 11:04

Neil P


People also ask

Is not a function in js error?

The Javascript error TypeError: "x" is not a function occurs when there is an attempt to call a function on a value or object, which is not actually a function.

Is not a function in jQuery error?

on if not a function" jQuery error, make sure to load a recent version of the jQuery library and check if you are including any scripts that are overriding the value for the global dollar sign $ variable after you load the jQuery script.

Is not a function WP?

$ is not a function WordPress error occurs when the code comes before the jQuery library. For example, if a plugin or theme calls a code before calling the right library, you get this error. By default, WordPress doesn't understand $ as jQuery and you have to make some modifications to fix this error.


1 Answers

Accessing a jQuery object using bracket notation returns a DOMElement which does not have the val() function. If you want to retrieve an element by its index within a matched set you need to use eq():

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

Alternatively you can retain the DOMElement and use the value property:

var Subject = myInputBoxes[i].value; 
like image 134
Rory McCrossan Avatar answered Sep 23 '22 17:09

Rory McCrossan