3

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.

hjpotter92
  • 78,589
  • 36
  • 144
  • 183
gsamaras
  • 71,951
  • 46
  • 188
  • 305

7 Answers7

2

The shortest notation would be:

$('li a span').data('tooltip', 'foo');
MaxZoom
  • 7,619
  • 5
  • 28
  • 44
1

I assume you want to do the following:

$('li a').find('span').data('tooltip', 'foo');
hjpotter92
  • 78,589
  • 36
  • 144
  • 183
1

Use .data():

$($(li).find('a').find('span').data('tooltip', 'foo'));
Mario Pabon
  • 605
  • 4
  • 8
1

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');

JCOC611
  • 19,111
  • 14
  • 69
  • 90
  • Two methods suggested, very nice! And the question is: which one should I use? Are they equivalent? What what would you suggest? – gsamaras Sep 28 '15 at 16:06
  • Thank @hjpotter92. JCOC611, you should update your answer with the gist of the link that jhpotter92 kindly provided, in order to make clear why we should prefer the one method over the other. That way your answer will be far better! – gsamaras Sep 28 '15 at 16:15
  • I am accepting this answer, because `.attr()` approach was the one that really worked in my actual application. – gsamaras Sep 28 '15 at 16:26
1

I think you can simplify that to:

$("li a span").data("tooltip", "foo");
sockmonk
  • 4,195
  • 24
  • 40
1

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

$($(li).find('a').find('span').data('tooltip', 'foo'));
gsamaras
  • 71,951
  • 46
  • 188
  • 305
user3753202
  • 517
  • 7
  • 14
  • Thank you, however, such kind of an answer is already provided. Next time, you will have to be faster! :) – gsamaras Sep 28 '15 at 16:14
1

Use simple with data-attribute

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