I want to select an option from select randomly.
<select class=".sel" id="sel">
<option>a</option>
<option>b</option>
<option>c</option>
<option>d</option>
</select>
actually i am using jQuery autocomplete.
Well,the question is how can i select option randomly from a select box ?
and what i tried is
function change_something(opt)
{
var randomOption=Math.floor(Math.random()*$(opt+' option').size());
$(opt+ option["value='"+randomOption+"'"]).attr('selected', 'selected');
}
Actually i am not a jQuery expert,so i am getting failed to change something .
Something like this should work:
var $options = $('#sel').find('option'),
random = ~~(Math.random() * $options.length);
$options.eq(random).prop('selected', true);
http://jsfiddle.net/elclanrs/8DPMN/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With