Issues Setting Value/Label Using DropKick Javascript

11,056

Solution 1

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" />

​

Solution 2

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.

Solution 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.

Solution 4

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

Solution 5

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 :)

Share:
11,056
kp_
Author by

kp_

Updated on June 04, 2022

Comments

  • kp_
    kp_ about 2 years

    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/