user957178
user957178

Reputation: 631

how to set the default value to the list box using jquery

I need to set the this as a default value for my list box can any body help me out.

thanks

  $(document).ready(function () {
            var targetid = '<%:ViewData["target"] %>';
            if (targetid != null) {
                $("#lstCodelist").val(targetid); //// I need to set this targetid as default selected value for lstCodelist
            }

Upvotes: 2

Views: 5236

Answers (4)

malik masis
malik masis

Reputation: 595

It is worked for me. You can try it

$("#foo").val("")

Upvotes: 0

$("#ListBox")[0].selectedIndex = 0

Upvotes: 0

Dan Short
Dan Short

Reputation: 9616

This should do it:

$('#foo').val(2)

http://jsfiddle.net/yRXEc/

Upvotes: 1

Diodeus - James MacFarlane
Diodeus - James MacFarlane

Reputation: 114377

Set n to the index of the item you wish to set as selected.

$("#lstCodelist option").eq(n).attr('selected','selected')

Upvotes: 3

Related Questions