MBurak Erman MBurak Erman - 4 months ago 9
jQuery Question

Select doesn't update

When I change select option, it doesn't update the value. The number of content inside box must change based of select option but it doesn't work.
Thanks in advance.
FIDDLE



$(function() {
var all = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz\-=_!#%&'*{},.\/:?\(\)\[\]@\\$\^*+<>~";
var myLength = $("#mySelect option:selected").val();

function randomAll() {
for (var i = 0; i < myLength; i++) {
var word = all[Math.floor(Math.random() * all.length)];
$('#content').append(word + i++);
}
}

$('#all').click(function() {
$('#content').html('');
randomAll();
});

});

#content {
width: 200px;
height: 50px;
border: 1px solid #333;
margin-top: 50px;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="all">All</button>
<div id="content"></div>
<select id="mySelect">
<option value="4" selected="selected">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>
</select>




Answer

get selected value inside the function

 function randomAll() {
        var myLength = $("#mySelect option:selected").val();
        for (var i = 0; i < myLength; i++) {
          var word = all[Math.floor(Math.random() * all.length)];
          $('#content').append(word + i++);
        }
      }

Demo