If your links point to the IDs of the dialog elements, and if you add a meta class choose
to each of them, you could combine the last three calls to:
$('a.choose').click(function() {
$(this.hash).dialog('open');
return false;
});
The HTML for one of those links is the most semantically correct and even works with JS disabled (assuming, the dialogs are there, then):
<a href="#chooseMoreCat" class="choose">Choose more categories</a>
The this.hash
part explained:
this
in the context of a jQuery event handling function is always the element, that the event appeared at. In our case, it's the clicked link. Note, that it's the DOM node, not a jQuery element.
this.hash
: DOM nodes, that correspond to HTML <a/>
elements, have certain special properties, that allow access to the target they're linking to. The hash
property is everything after (and including) an #
character in the URL. In our case, if the link points to the elements that should become dialogs, it's something like the string "#chooseMoreCnt"
.
$(this.hash)
is the jQuery function called for, e.g., "#chooseMoreCnt"
, which will select the appropriate div
.
For the dialog initialization, I would also go for classes:
$(".choose_dialog").dialog({
bgiframe: true,
autoOpen: false,
width: 500,
modal: true,
open: function(type, data) {
$(this).parent().appendTo(jQuery("form:first"));
}
});
Yes, it means to change the markup, but it also provides you with the freedom to
add any number of dialogs lateron
add any number of openers to any dialog lateron
style all dialogs and links to dialogs consistantly with minimal CSS
without touching the Javascript anymore.
If the dialogs are initiated differently (as mentioned in the comments), then you could go for this part with CuSS's $.each()
approach and read the appropriate width inside the function from an object defined elsewhere:
var dialog_widths = {'chooseMoreCat': 400, 'chooseMorePr': 300, /*...*/ };