jquery ui selectable: make an option selected on load
-
25-06-2021 - |
Question
I have the following HTML:
<ol id="selectable">
<li class="ui-state-default">Option 1</li>
<li class="ui-state-default">Option 1</li>
<li class="ui-state-default">Option 3</li>
</ol>
I need, for example the first option, to be selected by default on page load. I use the following code without any luck:
$(document).ready(function () {
$('#selctable li:first').addClass('ui-selected');
});
Any ideas on what I'm doing wrong?
Solution
You miss-spelled selectable.
$('#selectable li:first').addClass('ui-selected');
check out this jsFiddle
OTHER TIPS
You need to call selectable()
before adding the class to the first li
$(function() {
$( "#selectable" ).selectable().children().first().addClass('ui-selected');
});
attributeName - Use .attr() and set the value to true: http://api.jquery.com/attr/
$(document).ready(function () {
$('#selectable li:first').attr("selected",true);
});
See this question: jQuery Cannot set "selected"="selected" via attr() on <option> elements?
you can use like this
$('#selectable :first-child').addClass('ui-selected');
please check the example: FIDDLE
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow