javascript - Jquery year picker not working as expected -


i have created simple jquery year-picker, jelp of post , added custom code make yearpicker show specific value selected. code

html

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>  <select name="yearpicker" data-id="2010" class="yearpicker"> </select>  <select name="yearpicker"  data-id="2006" class="yearpicker"> </select> 

javascript

$(function () {    (i = new date().getfullyear() ; > 1900; i--) {        $this = $('.yearpicker');       if ($this.attr("data-id") != i) {          $this.append($('<option />').val(i).html(i));        } else {           $this.append($('<option selected/>').val(i).html(i));        }     } }); 

what want is, bind property named "data-id" selection control in end , when script runs want element selected in front end. , have multiple selection controls in page, reason first selections data-id selected on selections. detailed jsfiddle here. can see 2010 selected in both cases expect 2006 selected on second <select/> can 1 point out doing wrong?

try this:

$(function () {    $('.yearpicker').each(function(){       (i = new date().getfullyear() ; > 1900; i--) {          $this = $(this);          if ($this.attr("data-id") != i) {             $this.append($('<option />').val(i).html(i));          } else {             $this.append($('<option/>').val(i).attr("selected", "selected").html(i));          }       }    }); }); 

working fiddle


Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -