Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set dropdown selected value with backbone

Tags:

backbone.js

I want to set my dropdown menu from my js. Basically, i want to be able to set the updated drop down value from the js.

my.html

  <div class="dropDownList">
                    <select id="ddpFilter" >
                        <option value="1">Show 1</option>
                        <option value="2">Show 2</option>
                        <option value="3">Show 3</option>
                    </select>
                </div>

my.js

   events : {
     'change #ddpFilter' : 'Filter'
  },

  Filter : function(e){
      // Set Selected Value of the dropdown 
  }
});
like image 569
myTD Avatar asked Oct 17 '12 18:10

myTD


1 Answers

You should start your function names with a lowercase letter (as per Mr Crockford) and use more descriptive function names.

In order to set the value of a select list, use option[value='x'] syntax. Assuming you'd like to set the second option:

events : {
  'change #ddpFilter' : 'filterDdpSelect'
},

filterDdpSelect: function(e){
  var selectValue = 2;
  $("#ddpFilter option[value='" + selectValue + "']").attr("selected", "selected");
}
like image 106
Andy Avatar answered Nov 03 '22 21:11

Andy