mreq
mreq

Reputation: 6542

Coffeescript jQuery set data()

What am I doing wrong? I am trying to assign .data() to each anchor. Now, the script only removes the title, but doesn't store anything in data(). When I tried console.log(title) it wrote the title fine, but still didn't store it. I'm using jQuery 1.7.1

I have the following lines in coffee

$(document).ready ->
    initBubble();

initBubble = ->
    $('a[title]').each (index, element) => 
        setInfoAttr($(element))

setInfoAttr = (element) ->
    title = element.attr('title')
    element.data('info', title).removeAttr('title')

The compiled output is as follows

(function() {
  var initBubble, setInfoAttr;

  $(document).ready(function() {
    return initBubble();
  });

  initBubble = function() {
    var _this = this;
    return $('a[title]').each(function(index, element) {
      return setInfoAttr($(element));
    });
  };

  setInfoAttr = function(element) {
    var title;
    title = element.attr('title');
    return element.data('info', title).removeAttr('title');
  };

}).call(this);

Upvotes: 2

Views: 2009

Answers (1)

Sandro
Sandro

Reputation: 4771

Maybe shuffle things around a little bit. Only thing I can see is that the initBubble function may be undefined when it is called.

initBubble = ->
    $('a[title]').each (index, element) => 
        setInfoAttr $(element)

setInfoAttr = (element) ->
    title = element.attr('title')
    element.data('info', title).removeAttr('title')

// A little shorcut for $(document).ready
$ ->
    initBubble()

Upvotes: 2

Related Questions