0

When using jquery.selectbox.js plugin for making great looking SELECT dropdowns, I encountered a problem that I'm unable to select current value using JQuery: $("#select_id").val("my value");

The probable solution is to remove $('#select_id').selectbox(); call, but I lose the design.

What to do?

Investigation:

When using jquery.selectbox the real dropdown is hidden on the page and only its presentational elements made by jquery.selectbox are visible (actually they are DIVs). The problem is that when .val() is updated, corresponding visible DIV (with class="jquery-selectbox-currentItem") is not updated.

ronnyfm
  • 1,973
  • 25
  • 31
Paul
  • 25,812
  • 38
  • 124
  • 247
  • why do people downvote? is it so hard to understand questions like these can actually help someone later? thanks for this question and the solution! – Arturo Sep 12 '13 at 06:10

4 Answers4

3

Not the optimal solution, but cleaner that the workaround you proposed, try this (basically, force the selectBox to update its html):

$("#order_billing_region").val("your value").selectBox("options", $("#order_billing_region").html());
ronnyfm
  • 1,973
  • 25
  • 31
0

I have found a workaround this way, we can detach the selectbox, then set value and attach the selectbox plugin to the dropdown again.

function fnClearJquerySelectBox(control,value)
{
   $("#order_billing_region").selectbox("detach");
   $("#order_billing_region").val(value);
   $("#order_billing_region").selectbox("attach");
}

fnClearJquerySelectBox("control","0");

Hope this helps..

Himanth Kumar
  • 316
  • 2
  • 13
0

I found following: https://github.com/marcj/jquery-selectBox/blob/master/readme.md

Code: $('select').selectBox('value', 1);

ping
  • 663
  • 6
  • 13
0

I've invented a workaround:

var ctlName = "#order_billing_region";
var ctl = $(ctlName);
ctl.val("my value");
//A workaround for buggy jquery.selectbox
ctl.siblings(".jquery-selectbox-currentItem").html($(ctlName + " option:selected").html());
Paul
  • 25,812
  • 38
  • 124
  • 247