Mohamed Omezzine
Mohamed Omezzine

Reputation: 1104

get the attribute data-* from tag

Am trying to get the data-target attribute from img tags

Html:

<img src="my src" alt="my_alt" data-target= "my_target1" />
<img src="my src" alt="my_alt" data-target= "my_target2" />
<img src="my src" alt="my_alt" data-target= "my_target3" />
<img src="my src" alt="my_alt" data-target= "my_target4" />

Ruby:

my_html.css("img").each do |img|
 puts img[:src]
 puts img[:data-target]
end

img[:src] works fine but not the case for img[:data-target]

Upvotes: 0

Views: 366

Answers (2)

Marcelo De Polli
Marcelo De Polli

Reputation: 29291

It's only a matter of referencing symbols that have dashes (or other special characters) in them.

puts img[:'data-target']

Upvotes: 2

dirtydexter
dirtydexter

Reputation: 1073

for this type of data you will have to make the tags using the rails way

<%= content_tag(:img, :src => "#2012-09-29", :alt => "my_alt" :data => { :my_data => "my_target1" } ) %>

then you can access it using :my_data

Upvotes: 0

Related Questions