Reputation: 3590
If I had a jQuery object and wanted to call itself, is there any better way of doing that then like…
$("#notes").text($("#notes").data("defaulttext"));
I mean… I know I can do something like
var $notes = $("#notes");
to speed up things a bit, but I was just wondering if there is any variable I could use that means "the object I am currently working on", instead of manually typing in the object name again.
Upvotes: 1
Views: 52
Reputation: 31950
Pass a function inside .text()
method and in that function return your object's data attribute using this
object
$("#notes").text(function(){
return $(this).data("defaulttext")
});
Or in order to be less ugly you can create a plugin using
$.fn.edit = function() {
this.text(this.data('defaulttext'));
return this; //to maintain chainability
};
and then use like
$("css_Selector").edit();
to change the text of element to its data-defaulttext attribute
Upvotes: 5