7

I've been using a nice, elegant plugin called DropKick for my webapp http://jamielottering.github.com/DropKick/, and I seem to be having a slight issue with it and am not sure how to go about trying to fix it. I am trying to programmatically change the value of the select drop down menu. Below is a description of my issue, and a link to JSFiddle.

HTML:

<select id="start" class="timePreference">
   <option value="Choose">Choose</option>
   <option value="1">1</option>
   <option value="2">2</option>
   <option value="3">3</option>
   <option value="4">4</option>
   <option value="5">5</option>
   <option value="6">6</option>
   <option value="7">7</option>
   <option value="8">8</option>
   <option value="9">9</option>
   <option value="10">10</option>
   <option value="11">11</option>
   <option value="12">12</option>
</select>

jQuery:

 $('.timePreference').dropkick();

 $('#someDiv').click(function() {
    $('#start').val("1");
    alert($('#start').val());

 );

When I show the value in alert, it shows as one, however when I look at the labels on the option it stays at the default or whatever it was prior to the change.

For example, if my default was "Choose" and I click someDiv, then alert will show "1", so it changing, but the select dropdown will still show "Choose". Any suggestions. I may just be missing something small, not sure.

FSFiddle: http://jsfiddle.net/kdp8791/aNS9R/61/

kp_
  • 123
  • 1
  • 1
  • 9
  • hiya can you jsfiddle the issue please? I might be able to help you out, cheers! – Tats_innit Mar 25 '12 at 07:07
  • Here is link to the JSFiddle: http://jsfiddle.net/kdp8791/aNS9R/61/ – kp_ Mar 25 '12 at 16:31
  • Hiya Update mate: @kdp8791 Okies, I Have done a solution around this will update it in couple of hours when I will get enough time to Answer in detail, Issue was the css morphing which happens when dropkick styling is added, Will Answer your question with full detail and might optimize it as I go. (lol - this was in my head whole day :) good to see the whole thing working. :P cheers. – Tats_innit Mar 26 '12 at 04:11
  • Oh man! Life saver! I really appreciate the help!!!!! Can't express it enough :-) – kp_ Mar 26 '12 at 04:29
  • Anyone know how to fix the multiple widths issue? http://stackoverflow.com/questions/11769888/how-to-individually-target-multiple-dropdowns-in-css-for-dropkick-plug-in – user1318135 Aug 02 '12 at 22:55

5 Answers5

12

working demo : With Commnet http://jsfiddle.net/aNS9R/218/ && without comments only 7 lines needed: http://jsfiddle.net/aNS9R/220/

-phew-

So, to start with I tried Change event [of dropkick] with in drop kick but its only for the change event within select and not from external element binding. i.e. in your case change button.

So; this is what I have done:

Explanation (if you interested)

I used firebug to inspect the variable and found that dropkick marshal your existing select with nice styling now when you used $('#timePreference option:selected').val("1"); dropkick actually did changed the selected value with in your element with id=timePreference but the div and ul and li styling which is created by dropkick is not changed yet.

For the chosen span it has a class .dk_label and for the current (green color) is given by .dk_option_current class.

Please Note I pretty much read the plugin and figure out what is happening from here: https://github.com/JamieLottering/DropKick/blob/master/jquery.dropkick-1.0.0.js

If you wish to use firebug and see how elements are se use this link : http://jsfiddle.net/aNS9R/218/show/ and play around with your inspect mode, you will see dropkick styling and how it works.

JQuery code

 $(document).ready(function() {

    $('#timePreference').dropkick();

    $('#go').click(function(){

        // Assign slected option value to your select here - 
        // you can also make it something like this but your existing cdoe works anyways $("select_id option[value='3']").attr('selected','selected');
        $('#timePreference').val("1");

        //Now assign the select text value to the dropkick added element.
        //If you will use firebug you can see the nice <div>, <ul> & <li> structure which morph your dropdown.

        $('.dk_label').text(1);

      // further if you want green color to be selected. class=dk_option_current does that
      // You need to loop through the dropkick hierachy

      $(".dk_options_inner li").each(function(){

        $(this).removeAttr('class');
        if ($(this).text() == "1"){
           $(this).attr('class', 'dk_option_current');
        }
      });

    });
});
​

Hope this helps you mate, cheers!

HTML

     <select id="timePreference">
        <option value="Choose" selected="selected">Choose</option>
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
        <option value="4">4</option>
        <option value="5">5</option>
        <option value="6">6</option>
        <option value="7">7</option>
        <option value="8">8</option>
        <option value="9">9</option>
        <option value="10">10</option>
        <option value="11">11</option>
        <option value="12">12</option>
     </select>

    <input name="go" id="go" type="button" value="change" />

​
Tats_innit
  • 33,991
  • 10
  • 71
  • 77
  • This seemed to work, but in the event I had 2 dropkick's how can I only change one instead of both? – kp_ Mar 26 '12 at 13:57
  • Hiya, cooleos; so, every dropkick select has a unique id and name; you will use .closest api: http://api.jquery.com/closest/ for the select you want to change **further explanation** so if you inspect html using firebug every new dropkick for select had a unique id i.e. in the example above id=timepreference drop kick added this for nice presentation like this `
    ` hope it helps, cheers!
    – Tats_innit Mar 26 '12 at 19:40
  • 2
    Really appreciate the help Tats_innit. I ended up using children() instead of closest to find the proper label. It worked! http://jsfiddle.net/aNS9R/260/ – kp_ Mar 26 '12 at 22:29
  • No problems! glad it worked! unless the api grows for this plugin this work around should be fine. have a good one, cheers! – Tats_innit Mar 26 '12 at 22:45
  • @kdp8791 Thanks for your children jsfiddle, worked great for me with multiple dropkicks on a page! – Nathan May 09 '13 at 14:31
7

You can set the dropkick value programmatically by jQuery trigger of a click event on the selected dropkick option.

$option = $('#dk_container_'+ k +' .dk_options a[data-dk-dropdown-value="'+ v +'"]');

$option.trigger("click");

Here k is the select id or name, and v is the selected value.

The click event of dropkick will automatically take care of setting the classes of options to reflect the change.

jonsca
  • 10,218
  • 26
  • 54
  • 62
catimos
  • 156
  • 1
  • 6
3

I'm a bit late answering this but I have recently come across the same issue - updating a Dropkick DDL after it has been created. I have taken Tats_innit's code and modified it slightly, creating a function that allows you to simply pass in the ID of the select element and the value you want to change it to.

function updateDropkickDDL(id, value) {

    //Get the select element and dropkick container
    var select = $(id);
    var dk = select.prev('.dk_container');

    //Set the value of the select
    select.val(value);        

    //Loop through the dropkick options
    dk.find('.dk_options_inner').children("li").each(function () {

        var li = $(this);
        var link = li.children('a');

        //Remove the 'current' class if it has it
        li.removeClass('dk_option_current');

        //If the option has the value we passed in
        if (link.data('dk-dropdown-value') == value) {

            //Set the 'current' class on the option
            li.addClass('dk_option_current');

            //Set the text of the dropkick element
            dk.find('.dk_label').text(link.text());

        }

    });

}

You should now be able to simply call updateDropkickDDL on the click of a button or similar, for example, to set the value of the dropdown in your question to 1 you would use:

$(document).ready(function(){

    $('#start').dropkick();

    $('#someDiv').click(function(){

        updateDropkickDDL('#start', 1)

    });

}

This function also allows the use of multiple dropkick dropdown lists on the page, only updating the specified dropdown.

I hope this will help someone else encountering this issue.

patg
  • 277
  • 2
  • 9
3

I know this question is a few months old, but for anyone looking this up later I wanted to add this link to a pull request on the DropKick repository that adds in a "reverse sync" option that updates the custom dropdown menu whenever the underlying select object changes. That allows you to just update the select object in your code and the DropKick custom dropdown updates on the "change" event.

https://github.com/JamieLottering/DropKick/pull/27

LocalPCGuy
  • 6,006
  • 2
  • 32
  • 28
  • Thanks! Looks like this has now been merged into the master. I had to change `$('#seasons-filter').val("*")` to `$('#seasons-filter').val("*").change();` to get it to fire, but this is by far the best option for me. – Hobo Apr 28 '14 at 15:13
2

best way to change value
just add to jquery.dropkick-X.X.X.js after

methods.reset = function () {
...
};

this code

// change value of current select
// usage: $("...").dropkick('select', select_value);
methods.select = function (value) {
for (var i = 0, l = lists.length; i < l; i++) {
  var
    listData  = lists[i].data('dropkick'),
    $dk       = listData.$dk
  ;
  if ($(this)[0] == $dk.next()[0]){
    var $current  = $($dk.find('li a[data-dk-dropdown-value="' + value + '"]')[0]).closest('li');
    $dk.find('.dk_label').text(listData.label);
    $dk.find('.dk_options_inner').animate({ scrollTop: 0 }, 0);

    _setCurrent($current, $dk);
    _updateFields($current, $dk, true);
    var data  = $dk.data('dropkick');
    var $select = data.$select;
    $select.val(value);

    if ($.browser.msie)
      $current.find('a').trigger('mousedown');
    else
      $current.find('a').trigger('click');
    break;
  }
}
};

usage: $("...").dropkick('select', select_value);

pros
- native change state of dropkick object
- change selected option in original select
- trigger event, useful if you listen "change" state

cons
- need to change original library
- long code :)

iscariot
  • 434
  • 7
  • 14