Udders
Udders

Reputation: 6986

IE javascript troubles not firing event

I am having a problem with following script,

$('#colours li a').click(function(e) {

    $('option').attr("selected", false);

    var selected = $(this).parent("li");
    var colour = selected.attr("class");
    colour = colour.replace("rounded ", "");

  alert(colour);

    $('option[value=' + colour + ']').attr('selected',true);


    if($("#" + colour).hasClass("active")) { return; }

    var active = $(".active");
    var next = $("#" + colour);

    $('#colours li').removeClass("active");
    selected.addClass("active");

    $('option[value=' + colour + ']').attr('selected',true);
    e.preventDefault();
});

Basically what this does is on click it should select a value in a hidden select however in IE8 this is not happening, why is this?

Upvotes: 0

Views: 26

Answers (1)

charlietfl
charlietfl

Reputation: 171700

There is a simpler way to change the selected option by just changing the value of the parent select.

$('select').val(colour) will replace both of the attr() calls. Changing the value of a select automatically takes care of the selected change

attr() is not the preferred method for selected either, should use prop(). This is likely the root of the problem you are having

Upvotes: 1

Related Questions