During Jenkins build got this error : removeAttr is not a function

Viewed 32

I'm getting an error during the jenkins build:

TypeError: _myClassListID [i].removeAttr is not a function

var _myClassListID = jQuery(".myclass");
if (_myClassListID && _myClassListID.length > 0) {
   for (var i = 0; i < _myClassListID.length; i++) {
      myClassListID[i].removeAttr('role');
   }
}
1 Answers

Accessing a jQuery object by index returns an Element object, not a jQuery object. To achieve what you need use eq() instead:

let _myClassListID = $(".myclass");
if (_myClassListID && _myClassListID.length > 0) {
   for (var i = 0; i < _myClassListID.length; i++) {
      myClassListID.eq(i).removeAttr('role');
   }
}

However it's worth noting that the null check, length check and loop are all redundant. jQuery will execute most methods on all elements in the collection for you by default, and will not error if the selector returns no matches.

As such the all the code in your example can be reduced to just this:

$(".myclass").removeAttr('role');
Related