Reputation: 317
I have a select option that when it changes it activates a script. What I'm trying to do is at the start when there is a selected value in the select option It would also activate the script. Right now here is my code
I know that onload does not work on select option. So could you give me an idea for an alternative? Here is my code
<script>
function getState(val) {
var group = $("#group").val();
$.ajax({
type: "GET",
url: "<?=base_url()?>personnel/get_division/"+val,
data:{"group": group},
success: function(data){
$('#div1').html(data);
}
});
}
</script>
<select id="grp" name="group" class="cust_fields" onChange="getState(this.value);">
<option value=""> -- Select -- </option>
<?php $table = "tbl_group"; $select = "g_name, g_id"; ?>
<?php foreach($ddl1 as $row1){ ?>
<option value="<?=$row1['g_id']?>"> <?=$row1['g_name']?> </option>
<?php } ?>
</select>
Upvotes: 3
Views: 2502
Reputation: 4810
Since you have an onChange event tied to each element, you could trigger the event on the selected item on page load.
jQuery(document).ready(function($) {
//$('option:selected').trigger('change');
$('select').trigger('change');
});
Upvotes: 3