How do I make this function behave as I want it to? (Select onchange problem)
I have the following function:
- $("#paamelding_form select").change(function () {
$("option:selected").click(function() {
if ($(this).hasClass("double_0") || $(":selected").hasClass("")) {
var oldID = ($(this).parent("select").attr("id").match(/\d+$/));
var newID = ++oldID;
alert("remove" + newID);
$("#pulje_" + newID).removeAttr("disabled","disabled").css({ 'border-color' : ' #000', 'background-color' : '#ddd'});
$.cookie("pulje_" + newID +"","enabled", { path: '/' });
alert($.cookie("pulje_" + newID + ""));
$("label[for='pulje_" + newID + "']").css({ 'color' : '#2F5376'}).find(".double_warning").remove();
}
if ($(this).hasClass("double_1")) {
var oldID = ($(this).parent("select").attr("id").match(/\d+$/));
var newID = ++oldID;
alert("add" + newID);
$("#pulje_" + newID).attr("disabled","disabled").css({ 'border-color' : ' #777', 'background-color' : 'eee'});
$.cookie("pulje_" + newID +"","disabled", { path: '/' });
alert($.cookie("pulje_" + newID + ""));
$("label[for='pulje_" + newID + "']").css({ 'color' : '#2F5376'}).find(".double_warning").remove();
if($("option:selected").text() == 'Gopher') {
var double_warning = "<small class='double_warning'><span class='double_warning' style='color: red;'>Du valgte gopher i forrige delpulje og vil bli satt opp som gopher i denne delpuljen også</span></small>";
} else {
var double_warning = "<small class='double_warning'><span class='double_warning' style='color: red;'>Du valgte et spill som varer i to delpuljer, dette valget er derfor ikke tilgjengelig</span></small>";
}
$("label[for='pulje_" + newID + "']").css({ 'color' : '#aaa'}).append(double_warning);
}
});
- });
This works, but only for mouseclicks. It's probably also extremely crappy code to begin with - I'm pretty new to jquery, and I tend to go with what works for now - and this does. But I'd like it to work for both mouseclicks and when people navigate the form using the keyboard - any hints or improvements that can be done to the code to make it behave like I want it to?