user663724
user663724

Reputation:

How to set the data to the class and read it back?

To avoid usage of global variables , I am trying to set the data to the class and read it back . but as a result i am getting it back as [Object , Object] and when i tried to stringify that i am getting as {}

http://jsfiddle.net/oeufafed/2/

This is my code

<div id="section">
<form class="myformcalss">
London is the capital city of England. It is the most populous city in the United Kingdom
</form>
</div>

$('#section').find('.myformcalss').data('sample data');
var text = $('#section').find('.myformcalss').data();
//var result = JSON.stringify(text);
alert(text);

Could you please let me know how to set and read the data back ??

Upvotes: 0

Views: 61

Answers (1)

Oleksandr T.
Oleksandr T.

Reputation: 77492

You should use key

$('#section').find('.myformcalss').data('key', 'sample data');

var text = $('#section').find('.myformcalss').data('key');

for set some data

.data('key', 'value');

for get some data

.data('key');

key

Type: String,

A string naming the piece of data to set.

> > >

value

Type: Anything,

The new data value; this can be any Javascript type except undefined.

Example

Upvotes: 8

Related Questions