Cristiano Marques
Cristiano Marques

Reputation: 93

changing specific data from div

I have a div like this:

<div id="something" data-tip="tipeofdata"></div>

Then I want to change that data-tip with Jquery. I was trying like this:

$('#something').data('tip') = 'newdata';

But doesnt work. How can I do this?

Thanks.

Upvotes: 0

Views: 49

Answers (3)

KodeFor.Me
KodeFor.Me

Reputation: 13511

Try the following. This is what I am using and it works fine:

$('#something').attr('data-tip', 'newdata');

But also you can try the following:

$('#something').data('tip', 'newdata');

I have not tried that, but I suppose will work.

Upvotes: 0

Somnath Kharat
Somnath Kharat

Reputation: 3610

Try this:

$('#something').attr('data-tip','newdata');

Upvotes: 2

A. Wolff
A. Wolff

Reputation: 74420

Wrong syntax:

$('#something').data('tip') = 'newdata';

Should be:

$('#something').data('tip','newdata');

But that will change data object property value, not DOM node attribute

To change attribute on DOM node, you need to use:

$('#something').attr('data-tip','newdata');

Upvotes: 1

Related Questions