0

I try to to get text of number elements and put into array with jquery.but get this error that :

Uncaught TypeError: aTags[i].parent is not a function.

How to solve this?

    $('#flight-stops-filter ul li a').click(function() {
    var aTags=$('#flight-stops-filter ul li a');
    stops = [];
    for (var i = 0; i < aTags.length; i++) {
        if (aTags[i].parent("li").hasClass('active')) {
            alert(this.text);
            stops.push(this.text.substr(0, 1));
        }
    }

});
3
  • 1
    aTags[i] is DOM element wrap it with jquery wrapper..instead of aTags[i].parent(.. use $(aTags[i]).parent(.. Commented Apr 5, 2016 at 12:06
  • 1
    It looks like DOM not jQ wrapped element..use $(aTags[i]) Commented Apr 5, 2016 at 12:06
  • Or you should play with var aTags=$('#flight-stops-filter ul li'); and then in loop, $(this) Commented Apr 5, 2016 at 12:08

4 Answers 4

2

When you use bracket notation

 aTags[i].parent("li") 

it returns a DOM node

You want to use eq(index)

aTags.eq(i).parent("li")

Now inside the loop the use of this.text may be wrong too since there is no text property in DOM.

So what you probably want is

$('#flight-stops-filter ul li a').click(function() {
    var aTags=$('#flight-stops-filter ul li a');
    stops = [];
    for (var i = 0; i < aTags.length; i++) {
        var li = aTags.eq(i).parent("li");
        if (li.hasClass('active')) {
            console.log(li.text());
            stops.push(li.text().substr(0, 1));
        }
    }
});
Sign up to request clarification or add additional context in comments.

Comments

1

aTags is Jquery Object with pure DOM objects wrapped inside. So, you won't be able to invoke jquery methods on pure DOM objects without wrapping them inside jquery.

Replace your for loop with

   aTags.each(function(){
     if( $(this).parent("li").hasClass('active'))
     {
         alert(this.text);
         stops.push(this.text.substr(0, 1));
     }
   });

As suggested here in jquery learning documentation, rather than accessing via brackets notation you need to access it via eq method of jquery.

aTags.eq( 0 );//for first DOM object wrapped inside a jquery object

For accessing the DOM object

aTags.get(0) ;//same as aTags[0]; 

2 Comments

what happens if he has multiple selects?
@madalinivascu didn't get you... can you elaborate multiple selects?
0

Wrap the variable in a jQuery object (change aTags[i].parent to $(aTags[i]).parent):

if ($(aTags[i]).parent("li").hasClass('active')) {
   alert(this.text);
   stops.push(this.text.substr(0, 1));
}

or:

$('#flight-stops-filter ul li a').click(function() {
   var lis=$('#flight-stops-filter ul li.active');
   if (lis.length > 0 ) {
      alert(this.text);
   }
}

1 Comment

Makes zero sense to wrap it if you reference the element the right way to start
0

The cause of the error has been well explained by others. This alternative is both straight forward and there's no chance of you running into that error:

var links = $('#flight-stops-filter ul li a');
links.click(function() {
    stops = links.parent('.active').find('a').map(function() {
        return $(this).text().substr(0,1);
    }).get();
});

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.