OdeToCode IC Logo

What's Wrong With This Code? (#28)

Monday, August 15, 2011

Victor Brumble is writing code again, and this time he's writing a jQuery plugin. His plugin makes alert boxes easy, because Victor loves pointless, modal dialogs.

(function ($) {

    var settings = {
        text: 'Thank you for clicking!'
    $.fn.alerter = function (options) {

        return this.each(function () {

            if (options) {
                $.extend(settings, options);

            $(this).click(function () {

As you can see, Victor has a default text message associated with his plugin, but he also learned how to use jQuery.extend to allow callers to override the default text. The plugin is working well for Victor, but another developer is complaining about the following code not working correctly.

<p id="p1">
    This is paragraph 1.
<p id="p2">
    This is paragraph 2.
<script type="text/javascript">
    $("#p1").alerter({ text: "Please don't click again!" });
    $("#p2").alerter({ text: "This is not the text you want!" });

What could possibly be wrong?