Jesse Walton
Jesse Walton

Reputation: 147

Using jQuery, how can I store information in a dynamically created div?

I'm trying to create new objects from an attribute box with 'type', 'name' and 'number' text input fields. Ideally, you'd fill in the information, click create and a new object (100px by 100px box) would pop up.

Eventually, I'd like to get the text boxes to go blank when no object is selected, and then populate with the relevant info when one of the objects is selected.

Is there a way to store/retrieve information in a div?

Upvotes: 0

Views: 98

Answers (2)

Jabran Saeed
Jabran Saeed

Reputation: 6168

using the data attribute

Setting Value

$(div).data('id','value');

Getting Value

var id = $(div).data('id');

Upvotes: 1

Jason W
Jason W

Reputation: 13179

You can use attributes of an element.

var $element = $('div.mydiv');
$element.attr('data-type', myType);
$element.attr('data-name', myName);
$element.attr('data-number', myNumber);
console.log($element.attr('data-type')); // Show how to get value back

Upvotes: 1

Related Questions