Recently I have really been getting into the javascript library jQuery. The more I learn the more excited I get about how slick it is and so easy to use. It can really provide some rally cool features to your website and also provide a good way for me as a programmer to circumvent some tasks that can be hard to accomplish using server side programming.
Today I was presented with a task on an existing website using the DotNetNuke publishing platform. The customer has created a form using a module in the framework. The form is used to order some stuff and the visitor has to click a number of check boxes depending on what they want to order. There is also ha a bunch of pages with information about the products. Each page has a direct link on i to get to the order page. Now, if the visitor clicks this link the check box for that particular product should be checked, just to make thing easier.
I have no intention on going in to the framework to change the behaviour of the module so I thought I'd try some jQuery on this.
First off the only thing I really know is that the order form will have a header like this "<h1>Order form</h1>" (potentially the module can exist on multiple pages, even multiple times on the same page) and that the product page will have the product name in the url, which is also the text of the check box.
My idea was that first check if the order form exist on the page by looking for the header then check the referrer url and compare this to the check box names. This is the code:
$(document).ready(function()
{
if($("h1:contains(Order form)"))
{
setOrderCheckboxes();
}
});
// Automatically set checkboxes if the visitor previously viewed
// a page named the same as the checkbox
function setOrderCheckboxes()
{
$("input[type='checkbox']").each(function()
{
// The text element is always a sibling directly following the checkbox
var checkboxName = $(this).next().text();
// DNN url replaces characters in PageName
checkboxName = checkboxName.replace(" ", "").replace("-", "");
if(document.referrer.indexOf(checkboxName) != -1)
{
$(this).attr("checked", true);
}
});
}
Pretty slick right?