I am trying to change a data attribute, but it never seems to get changed and remains as "TEXT", which is the default.
function SuccessGetActorsForGroupCallback(data, el) {
var str = "";
jQuery.each(data, function (i, val) {
str += val + '<br />';
});
$(el).data('original-title', str);
Utilities.ApplyTooltips($(el));
}
Please help
To change the attribute value of an HTML element HTML DOM provides two methods which are getAttribute() and setAttribute(). The getAttribute() is used to extract the current value of the attribute while setAttribute() is used to alter the value of the attribute.
Creating an HTML Data Attribute Adding a data attribute is easy. Any HTML element can have any number of data attributes added to its opening tag. We simply type data- followed by the name of our attribute into the element's opening tag alongside any other attributes we're already using.
Element.setAttribute() Sets the value of an attribute on the specified element. If the attribute already exists, the value is updated; otherwise a new attribute is added with the specified name and value.
Data attributes will have a syntax like “data-*”. If you add the hyphen within the name of an attribute, Razor interprets this as a minus sign. To add a data attribute within an HTML helper, replace the hyphen with an underscore. Razor recognizes this and converts it to a hyphen.
With data-* attributes, you get that on/off ability plus the ability to select based on the value it has at the same specificity level. It’s the exact same as a class. We often think of specificity as a four-part value: So a single attribute selector alone is 0, 0, 1, 0.
When the field that you want to update is a calculated field, you cannot update data in the query. When the field that you try to update is read-only, the database is open as read-only, or the database is located on a read-only drive, you cannot update data in the query. To avoid this problem, do not open the database as read-only.
To work around this problem, add a primary key or a unique index to the linked table. When you do not have Update Data permissions for the query or the underlying table, you cannot update data. To resolve this problem, assign permissions to update the data.
Double click on the Active Directory Connector to view the Connector Space attributes. Click on the Preview button, on the following dialog click on the Generate Preview button. Now click on the Import Attribute Flow, this shows flow of attributes from Active Directory Connector Space to the Metaverse.
The .data
method doesn't alter the data-
HTML attribute; it alters a variable that jQuery stores internally.
If you really need/want to alter the data-
attribute, do so explicitly with the .attr()
method:
$(el).attr('data-original-title', str);
However, this won't alter the value returned by .data
. jQuery will fetch that value from the data-
HTML attribute only if it can't find the value stored internally. If you retrieve $(el).data('original-title')
again after altering the HTML attribute, you'll find it hasn't changed.
If this is a a problem, use the .removeData()
method to delete the internally-stored value. The next time you use .data()
, jQuery will see that it's missing and retrieve the data-
HTML attribute.
Take a look: http://jsfiddle.net/mblase75/LHCUK/
HTML:
<p id="p" data-title="blah"></p>
jQuery:
console.log($('#p').data('title')); // returns "blah"
// alter the attribute directly
$('#p').attr('data-title','whooo'); // data-title="whooo"
// test if .data is changed
console.log($('#p').data('title')); // still returns "blah"
// delete the .data() value
$('#p').removeData('title');
// now fetch it again -- it will retrieve the new data- attribute
console.log($('#p').data('title')); // returns "whooo"
Now, in practice, you shouldn't have to worry about this. Just remember that the data-
attribute represents the initial value of a .data()
variable and not necessarily the current value of that variable, and you'll be fine.
In summary: The .data()
method retrieves a value from the HTML element once when the document is loaded, and will not do so again as long as the variable is stored internally.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With