views:

32

answers:

1

In jQuery, I'm creating several div elements within a class:

function class([..])
{
  this.par1 = par1
  [..]

  // Create div
  $div = $('<div></div>')
  $div.attr({
    'id': 'someid' + this.par1,
    [..]
  })

  // Assign data to $div
  $div.data['par1'] = this.par1
  $div.data['this'] = this

  // Append to document
  $('#container').append($div)
}

The problem I have is that both .data['par1'] and .data['this'] are always the same when using firebug to get $('#someid1') e.g.

I also tried first adding it to the document, and later bind the data (after re-getting the jQuery object)

What am I doing wrong?

+1  A: 

Try div.data('par1',this.par1); for setting and div.data('par1'); for getting data.

Adrian Grigore
You're actually right, somehow I always used `.data['something']` which has worked for me in the past.
cpf