jQuery select selector
Blogs20122012-06-28
jQuery select selector
Here I list get/set jQuery
//add 'onChange' event for Select
$("#select_id").change(function(){//code...});
//get select's Text
var checkText=$("#select_id").find("option:selected").text();
//get select'sValue
var checkValue=$("#select_id").val();Â
//get select's index
var checkIndex=$("#select_id ").get(0).selectedIndex;
//get select's max index
var maxIndex=$("#select_id option:last").attr("index");
$("#select_id ").get(0).selectedIndex=1;
$("#select_id ").val(4);
$("#select_id option[text='jQuery']").attr("selected", true);
//append a Option
$("#select_id").append("<option value='Value'>Text</option>");
//unshift a Option
$("#select_id").prepend("<option value='0'>Select</option>");Â
//push a option
$("#select_id option:last").remove();Â
//shift a option
$("#select_id option[index='0']").remove();Â
//remove a option (val()=3)
$("#select_id option[value='3']").remove();
//remove a option (text()='4')
$("#select_id option[text='4']").remove();