Wordica
Wordica

Reputation: 2595

Get value of attribute

How can I get ID value:

for( var i=0; i<= 35; i++){
    var id = $('.image_carousel #' + i)
    var c = id.attr('id')
    console.log(c)
}

console.log give me 36 times "undefined". Why? what I do wrong here?

HTML

<% @post_images.each_with_index do |f,index| %>
        <div class='image_carousel' id='<%= index %>'><%= link_to 'abcde, '#',:class => "photo"  %></div>
    <% end %>

Upvotes: 0

Views: 55

Answers (1)

Quentin
Quentin

Reputation: 944441

You are looking for elements of various ids that are descendants of an element that is a member of image_carousel.

You should be looking for elements with those ids that are also members of that class.

Remove the descendant combinator (the space) from your selector.

var id = $('.image_carousel#' + i);

Note that since an id must be unique in the document, you can probably simply remove the class selector as well.

var id = $('#' + i);

Upvotes: 5

Related Questions