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?

1
  • You have a typo, missing an e in #selctable ? Commented Aug 11, 2012 at 7:53

5 Answers 5

5

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

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

FIDDLE

Sign up to request clarification or add additional context in comments.

1 Comment

+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.
3

You miss-spelled selectable.

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

check out this jsFiddle

1 Comment

Need to get some sleep I guess.
1

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

Comments

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

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

2 Comments

selected is for options of the select element.. he has an OL
Was my fault, I up voted your response, because the error is miss-spelled as you said
0

you can use like this

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

please check the example: FIDDLE

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.