cpf
cpf

Reputation: 1501

jQuery: Data on dynamic div's

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?

Upvotes: 0

Views: 261

Answers (1)

Adrian Grigore
Adrian Grigore

Reputation: 33318

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

Upvotes: 1

Related Questions