How is it possible to give a jquery-plugin individual local variables, that are accessable in different plugin-functions?
My script shows an alert with the content ‘123’, but I am expecting ‘abc’. So variable ‘t’ exists only once and not twice for each plugin. So for each plugin-instance there should be also an instance of variable ‘t’.
<html>
<head>
<title></title>
<script type="text/javascript" src="jquery/jquery-1.7.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js" type="text/javascript"></script>
<script type="text/javascript">
(function ($) {
var t = null;
$.fn.doSomething = function()
{
alert(t);
}
$.fn.myHtmlControl = function(option) {
t = option;
}
})(jQuery);
$(function () {
$('#ctrl1').myHtmlControl("abc");
$('#ctrl2').myHtmlControl("123");
$('#ctrl1').doSomething();
})
</script>
</head>
<body>
<div id='ctrl1'>Ctrl1</div>
<div id='ctrl2'>Ctrl2</div>
</body>
</html>
The usual way to do this is to use the
datafunction to store your information related to a specific element on the element itself. So in your case (live example):If you need to store multiple options, here’s a more robust example (live copy):