Guys here is my script to get value of selected option on change but not working as expected. on option change I want to get the value of the selected option but it returns all the previous and current value.Why so?
<div id="dropdown" style="height: 30px;width: 100%;text-align: center;">
Select website url:
<select name="selectsite" id="selectsite" style="border-radius: 5px; width: 300px;">
<option value="0">site1</option>
<option value="1">site2</option>
<option value="2">site3</option>
</select>
</div>
<br />
<button id="start" type="button">Start</button>
<button id="pause" type="button">Pause</button>
<script type="text/javascript">
$(document).ready(function () {
var selectval = $('#selectsite').val(); //get value of select option
//when first option is selected which is by default
if(selectval == 0) {
var selval = 0;
var injsArray = $.parseJSON('<?php echo json_encode($data); ?>');
var data = injsArray[selval]['dat']; //load the data
$('#start').on('click',function(){
playthis(selval, data);
});
}
$('#selectsite').on('change',function(e) {
var selval = $(this).val();
var injsArray = $.parseJSON('<?php echo json_encode($data); ?>');
var hdata = injsArray[selval]['dat']; //load the data
$('#start').on('click',function(){
playthis(selval,hdata);
});
});
function playthis(value,data){
console.log("Selected option value is "+value);
}
});
</script>
How to solve this problem? Please suggest if I am doing something wrong since I am new to programming. Thanks in advance