Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.GET() .prop() not working together jquery

Tags:

jquery

So the context is this:

I have some select inputs with cities, when the value changes I need to search for the ID of the parent node 9 levels above of that select input, then split it, and then get the last part of the array given by split.

I know the ID of the target node is in format "someName_SomeNumber" like "node_1234".

I've tried to do that on this function:

    $("select[name*=id_city_]").change(function(){
            alert($(this).parents().get(8).prop("id").split("_").last());
            });

everything works great if I execute "$(this).parents().get(8)", but when I try to get the ID with prop it says that prop is not a function.

thanks in advance.

like image 273
Xuaspick Avatar asked Apr 20 '15 14:04

Xuaspick


1 Answers

get() get's the underlying native DOM node, which doesn't have a prop method.

You want eq()

$(this).parents().eq(8).prop("id").split("_").pop()

or just use the native id property

$(this).parents().get(8).id.split("_").pop()

Also note that last() is a jQuery method, to get the last item in an array the native way, you pop it off the end

like image 160
adeneo Avatar answered Oct 12 '22 10:10

adeneo