0

Is there a way to change the value of an html variable using jquery.

For instance,

  <div id="smile" data-count="2"></div>

how would I change the variable data-count to 3 using jquery

2

2 Answers 2

0

You can do:

$('#smile').data('count', 3);

or:

$('#smile').attr('data-count', 3);

You can store the data value in a variable and then increase it like this:

var data = parseInt($('#smile').attr('data-count'), 10);

$('#smile').attr('data-count', data+1);
Sign up to request clarification or add additional context in comments.

5 Comments

Note that $('#smile').data('count', 3); doesn't change the attribute of the element, it only updates jQuery's internal data storage.
is it possible to just increment data-count by 1 without having to retrieve the data and manually adding one and then updating?
what does the number 10 do?
@KwaasiDjin: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…. And no, it's not possible to increment the value without getting it first.
@KwaasiDjin It's the radix which is a number (from 2 to 36) that represents the numeral system to be used, i.e. 2 for binary, 10 for decimal, 16 for hexadecimal. You can used @Felix Kling reference link for more details about it.
0
$('#smile').attr('data-count', 3);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.