fteinz fteinz - 1 month ago 18
jQuery Question

jQuery plugin base frame

I'm new to using jQuery. I tried to build a plugin, but it didn't work. Why?

Here is what I have:

<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
(function($){
$.alert = function(settings){
var config = {
// settings
'text': "old text",
'smily': ":(",
// ...
};
if ( settings ){$.extend(config, settings);}
// variables
var i = 0;
// script
alert(config.text + " " + config.smily);
};
</script>
</head>

<body>
<script type="text/javascript">
$(document).ready(function() {
$.alert(text: "new text", smily: ";)");
});
</script>
</body>

Answer

Assign the $.extend return value (similar question)

if (settings) { config = $.extend( {}, config, settings ); }

And call it like this:

$(document).ready(function() {
  $.alert( { text: "new text", smily: ";)" } );
});

Also remove the coma from the last default value 'smily': ":(",

Click here for a working demo.