0

So I have this piece of html

<td class="role-span"><span class="label"><% user.role %></span></td>

And I'm trying to get user.role value for each cell of the table like so

    $(document).ready(function() {
        $('.role-span').map(function() {
            alert($(this).text());
        })
    });

I'm able to get the span text value if I it is hard coded but if it's a <% %> it doesn't work anymore.

Any thoughts on that ?

Thanks

Manspider
  • 313
  • 3
  • 16

2 Answers2

1

<% %> will only execute ruby code but print result

Use <%= %> to print result.

Detail is here: https://stackoverflow.com/a/7996827/2549588

And the role-span is on a td so if you want to get all span's text you should do like this:

$(document).ready(function() {
    $('.role-span').map(function() {
        alert($(this).find('span.label').text());
    });
});

Or maybe you should use each instead of map, because you just want to do a loop not get an array.

Donald Chiang
  • 167
  • 1
  • 1
  • 13
1

As mentioned by @donald above you need to replace

<td class="role-span"><span class="label"><% user.role %></span></td>

with

<td class="role-span"><span class="label"><%= user.role %></span></td>

As

  • <% %> just evaluates the expression inside it whereas

  • <%= %> evaluates and print the result returned by the expression.

Deepak Mahakale
  • 19,422
  • 8
  • 58
  • 75