gsamaras
gsamaras

Reputation: 73366

Modify value of attribute

How to modify the value of data-tooltip? I would like to do that with one line of code.

Here is the element:

<li class="ui-btn-icon-left " data-icon="cf">
  <a href="#" class="ui-btn ui-icon-cf">
    <span class="tooltip" data-tooltip="NeverGiveUp">Samaras</span>
  </a>
</li>

Here are two of my attempts:

$($(li).find('a').find('span').attr('data-tooltip')).val('foo');
$($(li).find('a').find('span').attr('data-tooltip')).text('foo');

which had no impact.

Upvotes: 3

Views: 68

Answers (7)

Đặng Văn Thanh
Đặng Văn Thanh

Reputation: 558

Use simple with data-attribute

$('.tooltip[data-tooltip]').data('tooltip', 'foo');

Upvotes: 1

hjpotter92
hjpotter92

Reputation: 80629

I assume you want to do the following:

$('li a').find('span').data('tooltip', 'foo');

Upvotes: 1

user3753202
user3753202

Reputation: 525

For HTML 5 data-attributes, you can use data()

$($(li).find('a').find('span').data('tooltip', 'foo'));

Upvotes: 1

JCOC611
JCOC611

Reputation: 19709

Well the straight forward answer is .attr('data-tooltip', 'new value');. However, it is also possible, and more recommended (see hjpotter92's comment), to use .data('tooltip', 'new value');

Upvotes: 1

MaxZoom
MaxZoom

Reputation: 7743

The shortest notation would be:

$('li a span').data('tooltip', 'foo');

Upvotes: 2

sockmonk
sockmonk

Reputation: 4255

I think you can simplify that to:

$("li a span").data("tooltip", "foo");

Upvotes: 1

Mario Pabon
Mario Pabon

Reputation: 605

Use .data():

$($(li).find('a').find('span').data('tooltip', 'foo'));

Upvotes: 1

Related Questions