Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
I need to set property for the elements. Also, I am quite confused with how to use jQuery plugins. How can I call a function inside a jQuery plugin from outside and print the result in log.
To call a function inside a jQuery plugin from outside, try to run the following code. The code updates the name with jQuery as an example using properties:
<html>
<head>
<script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$.fn.person = function(prop) {
var defaults = $.extend({
name: 'Amit'
}, prop);
// methods to be used outside of the plugin
var person = {
changeName: function(name) {
defaults.name = name;
},
getName: function() {
return defaults.name;
}
};
return person;
};
// set initial properties
var person = $('#person').person({
name: 'Sachin'
});
document.write('Initial name of person: ', person.getName());
person.changeName('Amit');
// printing new name
document.write('<br>Changed name of person: ', person.getName());
</script>
</head>
<body>
<div id = "person">Changes done above!</div>
</body>
</html>