Skip to content Skip to sidebar Skip to footer

Jquery Update Data- Attribute/value

I have a DIV that looks like this...utilizing the 'data-gauge' attribute.
Copy

Then access it like

var foo= $( "#uib-justgage-3" ).data( "foo");
var bar= $( "#uib-justgage-3" ).data( "bar");

Here's a little example, it sets the data, then reads it when you click:

    $("#uib-justgage-3").data("foo", 52);
    $("#uib-justgage-3").data("bar", {
      myType: "test",
      count: 40
    });

    $('#b').click(function() {

      var foo = $("#uib-justgage-3").data("foo");
      var bar = $("#uib-justgage-3").data("bar");

      $('<span>').html("foo: " + foo).appendTo($("#uib-justgage-3"));
      $('<span>').html("<br>bar.myType: " + bar.myType).appendTo($("#uib-justgage-3"));
      $('<span>').html("<br>bar.counte: " + bar.count).appendTo($("#uib-justgage-3"));

    });
<scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script><divid="uib-justgage-3"></div><buttonid="b">Get data</button>

Solution 2:

You could use either $("#uib-justgage-3").data('gauge') or $("#uib-justgage-3").attr('data-gauge') for reading the attribute

and

$("#uib-justgage-3").data('gauge','new value') or $("#uib-justgage-3").attr('data-gauge','new value') for setting the attribute

Solution 3:

$('#uib-justgage').data({'rpath','Hello'});//sets data-rpath = Hello
$('#uib-justgage').data('rpath');//gets the value assigned to 'data-rpath'

Post a Comment for "Jquery Update Data- Attribute/value"