I am trying to check various checkboxes on a form based on parameters found in the URL.
The URL could resemble something like this:
https://development.mycompany.com/project/home.php?profile=profile1,profle2®ion=&rep=
As you will see, the "profile" parameter in the URL has two values (profile1, profile2).
I have a form that has a series of checkboxes that looks like this:
<input class="profileCheckbox" type="checkbox" id="PROFILE1CHECK" name="profileCheckbox" value="PROFILE1" />
<input class="profileCheckbox" type="checkbox" id="PROFILE2CHECK" name="profileCheckbox" value="PROFILE2" />
<input class="profileCheckbox" type="checkbox" id="PROFILE3CHECK" name="profileCheckbox" value="PROFILE3" />
// few more
Using the URL parameters, the checkbox ID's PROFILE1CHECK and PROFILE2CHECK should be checked, leaving any other checkboxes unchecked.
I have this piece of codes that grabs any parameters found in the URL:
var getUrlParameter = function getUrlParameter(sParam) {
var sPageURL = window.location.search.substring(1),
sURLVariables = sPageURL.split('&'),
sParameterName,
i;
for (i = 0; i < sURLVariables.length; i++) {
sParameterName = sURLVariables[i].split('=');
if (sParameterName[0] === sParam) {
return typeof sParameterName[1] === undefined ? true : decodeURIComponent(sParameterName[1]);
}
}
return false;
};
var profile = getUrlParameter('profile');
console.log(profile);
When I console the profile variable, I can see the following:
profile1,profile2
This is where I got stuck.
I'm not really sure how to check the checkboxes for the ID's for PROFILE1CHECK and PROFILE2CHECK.
var profile = getUrlParameter('profile');
$("#PROFILE1CHECK").attr("checked",profile.indexOf("profile1") !== -1);