3

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?

Daniil Ryzhkov
  • 7,416
  • 2
  • 41
  • 58
Subliminal Hash
  • 13,614
  • 20
  • 73
  • 104

5 Answers5

5

You need to call selectable() before adding the class to the first li

$(function() {
    $( "#selectable" ).selectable().children().first().addClass('ui-selected');
});​

FIDDLE

adeneo
  • 312,895
  • 29
  • 395
  • 388
  • +1 for help on adding a selection to the selectable programmatically. There's no guidance on this in the jQuery Ui Selectable documentation. I would believe this would be a pretty common requirement. – JustinMichaels Feb 18 '13 at 21:51
3

You miss-spelled selectable.

$('#selectable li:first').addClass('ui-selected');​

check out this jsFiddle

Amin Eshaq
  • 4,034
  • 1
  • 17
  • 21
1

attributeName - Use .attr() and set the value to true: http://api.jquery.com/attr/

Compustretch
  • 127
  • 2
  • 11
0
$(document).ready(function () {
    $('#selectable li:first').attr("selected",true);
});

See this question: jQuery Cannot set "selected"="selected" via attr() on <option> elements?

Community
  • 1
  • 1
Alberto León
  • 2,879
  • 2
  • 25
  • 24
0

you can use like this

$('#selectable :first-child').addClass('ui-selected');

please check the example: FIDDLE

Pradeeshnarayan
  • 1,235
  • 10
  • 21