9

I am trying to change data-progress with jquery by

doing this

$("#changeme").data('progress','100');

I've also tried

$("#changeme").attr('data-progress','100');
$("#changeme").prop('data-progress','100');

It will not work and I do not know what to do.

<div id="changeme" style="padding-left:10px;" class="tox-progress" data-size="120" data-thickness="8" data-progress="23" data-color="#229922" data-background="aqua" data-speed="500">
    <div class="tox-progress-content" data-vcenter="true">
        <p style="padding-left:40px;padding-top:10px;"></p>
    </div>
</div>

UPDATE: I really appreciate all your help, but I've tried everything suggested and still will not update it. It is a circular progress bar and data-progress tells it how much to fill up. It will not change from its initial value that I set in the beginning.

UPDATE 2: I fixed it! I forgot about the script that activates the circular progress bar which was in the header and being loaded before document.ready. Thanks for your help everyone!

5
  • is this your complete js code? Commented Jul 11, 2018 at 6:06
  • no its part of a large amount of code Commented Jul 11, 2018 at 6:11
  • $("#changeme").attr({'data-progress','100'}); just add attr({}) then your code. Commented Jul 11, 2018 at 6:17
  • Have you put your jquery in document.ready bloack? Commented Jul 11, 2018 at 6:37
  • yes I put it inside $(function (){}); Commented Jul 11, 2018 at 6:39

6 Answers 6

14

You need to read more about jQuery functions .data() and .attr() as the documentation will help you understand the difference between .data and .attr

.data() : Store arbitrary data associated with the matched elements or return the value at the named data store for the first element in the set of matched elements.

.attr() : Get the value of an attribute for the first element in the set of matched elements or set one or more attributes for every matched element.

For example if you have this DOM element

<a id="link1" data-test="initvalue" href="#">link1!</a>

try to evaluate these values

console.log( $('#link1').data('test') );
//outputs "initvalue"

// update the old value 
$('#link1').data('test', 'newvalue');

console.log( $('#link1').attr('data-test') );
//outputs "initvalue" as the attribute was never changed

console.log( $('#link1').data('test') );
//outputs "newvalue" as the value has been updated on the object
Sign up to request clarification or add additional context in comments.

Comments

4

Demo

Reference

From the reference:

jQuery itself uses the .data() method to save information under the names 'events' and 'handle', and also reserves any data name starting with an underscore ('_') for internal use.

It should be noted that jQuery's data() doesn't change the data attribute in HTML.

So, if you need to change the data attribute in HTML, you should use .attr() instead.

<div id="changeme"  style="padding-left:10px;" class="tox-progress" data-size="120" data-thickness="8" data-color="#229922" data-background="aqua" data-progress="23" data-speed="500">
<div class="tox-progress-content" data-vcenter="true">
    <p style="padding-left:40px;padding-top:10px;"></p>
</div>
Js
var a = $('#mydiv').data('data-thickness'); //getter

$('#mydiv').data('data-thickness',20); //setter

Comments

2

To be clear,

  1. attr() method replaces the DOM property with the given value and are restricted to only string types.
  2. data() method is used to store arbitrary info of any valid datatype, related to the elements. They cannot be accessed via DOM manipulation.

How attr() works ? Official Documentation

Setting values: Its sets the values of the attributes of a HTML element DOM. The changes will be reflected immediately and can be seen in the console (Inspect Element)

(1) $(element).attr(key,value) - The attribute-value pair to be set on the element.

(2) $(element).attr(obj) - An object of attribute-value pairs to be updated to an element.

(3) $(element).attr(key,<callback_function>) - The value to be updated to an attribute of an element is returned from the callback function.

Reading values: $(element).attr(key)

It returns the attribute value in the DOM. Its recommended to use attr() for only attributes and prop() for the properties.

Version dependency (both setting and reading): Prior to version 1.6, properties can also be added via attr() method. From version 1.6, jQuery provides prop() method to update the properties of an element. Like checked, disabled, selected etc...


How data() works ? Official Documentation

Storing values: $(element).data(key,value)

It stores the info in the cache ($.cache) mapped to a unique jQuery id for each element. The unique id starts from 0 and gets incremented for every new element. These properties will not be updated in the DOM. So using vanilla Javascript HTMLElement.dataset[key] or HTMLElement.getAttribute("data-*") will return nothing.

For data- attributes, the keys are camelCased before storing in the cache.

Version dependency: Prior to jQuery 1.4.4, the method completely replaces the data object instead of extending it with the elements attributes.

Reading values: (1) $(element).data(), (2) $(element).data(key)

(1) Returns all the data assigned to the element.

Version dependency: As said already, prior to version 1.4.4, the method returns the values only set by $(element).data(key,value), no data- attributes in the HTML will be returned. So it is recommended to use the latest version.

(2) Returns the value assigned to the key on the element. If the key is present in the cache, it is returned else the concerned data- attribute is returned.

<div id="data" data-progress-bar="20"></div>

$("#data").data("progress-bar",100);
console.log($("#data").data("progress-bar")); //consoles 100

$(document).ready(function(){
    console.log($("#data").data("progress-bar"));
    console.log($("#data").data("progressBar"));
    console.log($("#data").data()["progress-bar"]);
    console.log($("#data").data()["progressBar"]);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="data" data-progress-bar="20"></div>

In the above snippet,

$(element).data("progress-bar") - returns 20 - gets the data from the data- attributes.

$(element).data("progressBar") - returns 20 - converts the camelCased string to hyphenated and gets the data from the data- attributes.

$(element).data()["progress-bar"] - gets the data object from cache and but returns undefined, as the key will not be present. The difference between this and the second approach is, the second approach converts the hyphenated string to camelCased internally.

$(element).data()["progressBar"] - gets the data object from cache and returns the value for the key.

So finally,

USING attr():

$(document).ready(function(){
    console.log("Before setting : "+$("#data").attr("data-progress"));
    $("#data").attr("data-progress",100);
    console.log("After setting : "+$("#data").attr("data-progress"));
    console.log("After setting (acessing via JavaScript) : "+document.querySelector("#data").getAttribute("data-progress"));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="data" data-progress="20"></div>

USING data():

$(document).ready(function(){
    console.log('Before setting : $("#data").data("data-progress") : '+$("#data").data("data-progress"));
    console.log('Before setting : $("#data").data("progress") : '+$("#data").data("progress"));
    $("#data").data("data-progress",100);
    console.log('After setting : $("#data").data("data-progress") : '+$("#data").data("data-progress"));
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js"></script>

<div id="data" data-progress="20"></div>

Comments

-1

It's working fine. check updated snippet below...

using .data()

$('#changeme').data('progress',100);
console.log("data-progress: " + $('#changeme').data('progress'));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="changeme"  style="padding-left:10px;" class="tox-progress" data-size="120" data-thickness="8" data-color="#229922" data-background="aqua" data-progress="23" data-speed="500">
  <div class="tox-progress-content" data-vcenter="true">
    <p style="padding-left:40px;padding-top:10px;"></p>
  </div>
</div>

using attribute

$('#changeme').attr('data-progress',100);
console.log("data-progress: " + $('#changeme').attr('data-progress'));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="changeme"  style="padding-left:10px;" class="tox-progress" data-size="120" data-thickness="8" data-color="#229922" data-background="aqua" data-progress="23" data-speed="500">
  <div class="tox-progress-content" data-vcenter="true">
    <p style="padding-left:40px;padding-top:10px;"></p>
  </div>
</div>

2 Comments

Do you know why it does not show up in html doc. I have it inside a document.load function
If you are using .data() then you can't check updated value through dev. tool.
-1

Use

.attr({"data-progress":"25"}),

basically you need to pass a object with key value , where key is attribute name and value is attribute value.

console.log("Before : "+$("#changeme").attr("data-progress"));

$("#changeme").attr({"data-progress":"25"});

console.log("After : "+$("#changeme").attr("data-progress"));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<div id="changeme"  style="padding-left:10px;" class="tox-progress" data-size="120" data-thickness="8" data-progress="23" data-color="#229922" data-background="aqua"  data-speed="500">
                                <div class="tox-progress-content" data-vcenter="true">
                                    <p style="padding-left:40px;padding-top:10px;"></p>
                                </div>

Comments

-1

Try this

<div id="changeme"  style="padding-left:10px;" class="tox-progress" data-size="120" data-thickness="8" data-color="#229922" data-background="aqua" data-progress="23" data-speed="500">
	<div class="tox-progress-content" data-vcenter="true">
		<p style="padding-left:40px;padding-top:10px;"></p>
	</div>
</div>

<script>
	document.getElementById('changeme').setAttribute('data-progress', 100);
</script>

Using jquery for this isn't actually needed. You can just use javascript for this. The only time I would use jquery is if I need it, other wise native javascript will do just fine

An important thing to remember is to make sure your javascript is at the end of the page. That way it will see the dom element that you want to manipulate

I often ran across this problem where the script doesn't work even though it is correct and there is no mistake. Only to find out that I need to put it inside an onload event, $(document).ready, at the end of the page or below the dom element that I want to manipulate

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.