I’m just not getting this for some reason. I’ve tried various ways of using quotes, etc, but nothing works so far.
The URL I’m passing to the page is:
http://dev.bizzocall.com/indx-signup.php?plan=‘#Flexi’
I’ve also tried
http://dev.bizzocall.com/indx-signup.php?plan=#Flexi
http://dev.bizzocall.com/indx-signup.php?plan=Flexi
http://dev.bizzocall.com/indx-signup.php?plan='Flexi'
(and writing the switch statement differently to accomodate the differences….
What am I missing?
(function($) {
$.QueryString = (function(a) {
if (a == "") return {};
var b = {};
for (var i = 0; i < a.length; ++i)
{
var p=a[i].split('=');
if (p.length != 2) continue;
b[p[0]] = decodeURIComponent(p[1].replace(/\+/g, " "));
}
return b;
})(window.location.search.substr(1).split('&'))
})(jQuery);
$(document).ready(function() {
$myplan = $.QueryString["plan"];
switch ($myplan) {
case "#Flexi":
$aSel="#Flexi";
break;
case "#Grow":
$aSel="#Grow";
break;
case "#Excel":
$aSel="#Excel";
break;
case "#Max":
$aSel="#Max";
break;
}
($aSel).addAttr('selected', true)
.siblings('option')
.removeAttr('selected');
});
If you want an option to be set when the page loads, it’s better to do it serverside; if the user has JS disabled, the option won’t be set. You said you’re using PHP in a comment, so I’ll give an example using that.
For these kinds of situations, I like to use an array of options to print out the
<select>box, a bit like this:The code above loops through an array of values. When it finds a key in the array that matches
$selectedOptionit gives that<option>theselected="selected"attribute. This method saves having to type out a huge load of HTML with bits of PHP everywhere. I usually put this code in a function and pass it an associative array and a selected option, but it’s entirely up to you.