Skip to main content

jQuery Tips

The following are useful tips for handling jQuery - Javascript library 1. To change the selected option based on the option tag's value , use the following code.

$('#element option[value="no"]').attr("selected", "selected");

However, if multiple options can be selected, first deselect them using the following code
HTML MarkUp 
-- Javascript with jQuery --
$("document").ready(function() {
    $("#b1").click(function() {
        $(".selDiv select.opts option").each(function() {
            $(this).removeAttr("selected");
        });
        var optSelValue = $("#txt").val();
        $(".selDiv select.opts option[value=\"" + optSelValue + "\"]").attr("selected", "selected");
    });
});
Full Sample

Comments