RubyRedGrapefruit
RubyRedGrapefruit

Reputation: 12224

How can I get I fix this jQuery loop to give me what I want?

Note: This is CoffeeScript.

I've tried every iteration of this I can think of.

I want to loop through child elements and get the ids of the select child of each:

$(this).closest(".control-group").nextUntil("#attribute_list").each (key, ele)->
  console.log $(ele).children("select").attr("id")

I keep seeing "undefined" in the console log.

That element HAS a single child that is a select. I just can't get it. No matter what I do.

Upvotes: 0

Views: 46

Answers (2)

agconti
agconti

Reputation: 18093

Try:

$( @ ).closest ".control-group"
    .nextUntil "#attribute_list" 
    .each ( key, ele ) ->
        console.log( $( ele ).find( "select" ).attr( "id" ) )

note: this assumes the latest coffeescript compiler 1.7

Use this to upgrade:

sudo npm install -g coffee-script

Upvotes: 1

dave
dave

Reputation: 64657

I think you should be using $(this) in this instance:

$(this).closest(".control-group").nextUntil("#attribute_list").each->
  console.log $(this).children("select").attr("id")

Upvotes: 1

Related Questions