I have a select element
<select class="form-control position" name="position">
<option value="" selected disabled hidden>Pick A Position</option>
<?php
$option = 0;
foreach ($jsonIterator as $key => $val) {
if(is_array($val)) {
$option++
?>
<option value="<? echo $option; ?>"><? echo $key; ?></option>
<?php }}?>
</select>
I have 3 buttons, they have value 1, 2 and 3 respectively
<button type='button' value='1' class='btn-primary pull-left apply'>Apply</button>
I have the following JS
<script>$(document).ready(function () {
$('.position').chosen();
$(".apply").click(function () {
var id = $(this).val();
$('html,body').animate({
scrollTop: $("#contact").offset().top }, 'slow');
$.ajax
({
type: "POST",
url: "contact.php",
data: id,
cache: false,
success: function (html) {
$(".positon").val(id).trigger('chosen:updated');
}
});
})
});
When I click on a button, the default option of ChosenJs is not changing
update your option values,
HTML
<select class="form-control position" name="position">
<option value="" selected disabled hidden>Pick A Position</option>
<?php $option = 0; foreach($jsonIterator as $key => $val){ ?>
<?php if(is_array($val)){ $option++; ?>
<option value="<?= $option; ?>"><?= $key; ?></option>
<?php }}?>
</select>
and pass the select value to ajax,
jQuery
$(function(){
$('select[name="position"]').chosen();
$(".apply").click(function () {
var id = $(this).val();
$('html,body').animate({ scrollTop: $("#contact").offset().top }, 'slow');
$.ajax({
type: "POST",
url: "contact.php",
data: { id : id },
cache: false,
success: function (html){
$('select[name="position"]').val(id).trigger("chosen:updated");
}
});
});
});