1

I'm sorry for the general title, but I don't realy know how to call it.

Here is my code, that I want to reduce:

    $('.ShowDialogIcon').click(function() {
        $('.show_dialog').slideDown('fast');
        return false;
    });

    $('.ShowDialogIcon2').click(function() {
        $('.show_dialog2').slideDown('fast');
        return false;
    });

    $('.ShowDialogIcon3').click(function() {
        $('.show_dialog3').slideDown('fast');
        return false;
    });

    $('.ShowDialogIcon4').click(function() {
        $('.show_dialog4').slideDown('fast');
        return false;
    });

Now I see, that this is a bit unefective, what I would like to do is:

$('.ShowDialogIcon').showDialog('.showDialog');
$('.ShowDialogIcon2').showDialog('.showDialog2');
$('.ShowDialogIcon4').showDialog('.showDialog3');
$('.ShowDialogIcon4').showDialog('.showDialog4');

And what I've come up with is:

jQuery.fn.showDialog = function(cls) {
    $(this).click(function() {
        $(cls).show('fast');
    });
}

And it doesn't work, can anyone help please?

Thanks, Mike.

2
  • In what way doesn't it work? What do you see? What errors are reported? Commented Jan 11, 2011 at 18:05
  • 2
    You're using two different classes in your examples. .show_dialog2 in the current code and .showDialog2 in the desired code. Intentional? Commented Jan 11, 2011 at 18:07

3 Answers 3

5

You're setting click event on the wrong element.

The correct jQuery plugin

This is an equivalent to your functionality:

jQuery.fn.extend({
    showDialog: function(cls) {
        this.click(function(evt){
            evt.preventDefault();
            $(cls).slideDown("fast");
        });
    }
});

To stop event bubbling it's better to use preventDefault function on the event itself than returning false. This way you can safely avoid browsers' particularities.

Sign up to request clarification or add additional context in comments.

9 Comments

You're setting jQuery.fn.extend to an object, rather than calling it on the object...
@sberry2A: Thanks. My thoughts exactly. ;)
Apart from the performance benefit of this vs $(this), this should be equivalent to the code above, so this probably isn't the solution, but it's much nicer code!
This one looks good, but when I use it like: $('.ShowDialogIcon').showDialog('.showDialog'); It stops the link, but nothing slides down =/
@Mike See Stephen's comment above.
|
0

If you want to set up a jQuery plugin to do this:

(function($) {
    $.fn.showDialog = function(cls) {
        this.click(function() {
            $(cls).show('fast');
            return false;
        });
    });
})(jQuery);

$("#foo").showDialog(".bar");

See here for how to author a plugin.

1 Comment

@Robert: Good point, but easier to do it now in case I decide to add some private variables later.
0

My usual approach to this situation is first give a class that all dialog icons share and add an attribute to the dialog icon that points to the dialog that you want to show. For instance:

<a href="#show_dialog1" class="ShowDialogIcon">Show first</a> //href points to the dialog that will slide
<a href="#show_dialog2" class="ShowDialogIcon">Show first</a>  

$(".ShowDialogIcon").click(function(){
    //use the href attribute
    var dialog = $(this).attr("href").replace("#",".");  
    $(dialog).slideDown("fast");  //slidedown
    return false; 
});

1 Comment

i'm guessing you don't like that much then.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.