Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
144
rated 0 times [  149] [ 5]  / answers: 1 / hits: 65390  / 9 Years ago, fri, april 24, 2015, 12:00:00

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?


More From » jquery

 Answers
19

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;

[#66915] Thursday, April 23, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
prestonh

Total Points: 384
Total Questions: 105
Total Answers: 105

Location: England
Member since Tue, Sep 8, 2020
4 Years ago
;