I want a combobox by default selected the last option (using jquery):
Just using vanilla Javascript you can combine .selectedIndex and .length properties of the < select > dom object in order to achieve this:
document.querySelector("#mySelect").selectedIndex = document.querySelector("#mySelect").length - 1;
<select id="mySelect">
<option>item1</option>
<option>item2</option>
<option>item3</option>
<option>item4</option>
<option>item5</option>
</select>
<select>
<option>item1</option>
<option>item2</option>
<option>item3</option>
<option>item4</option>
<option selected="selected">item5</option>
</select>
Do something like this:
$(function() {
$("select option:last").attr("selected", "selected");
});
A plain JavaScript solution:
select.selectedIndex = select.options.length-1;
Demo
Use "prop" instead of "attr", of course depending of what version of jQuery you are using.
$("select option:last").prop("selected", "selected");
jQuery prop: http://api.jquery.com/prop/
More discussion on the subject of when to use prop or attr: .prop() vs .attr()