Reputation: 2439
What I am trying to do is that if the value of the key matches the php code I want it to add a selected attribute in the option. is it possible to collaborate php and jquery?
$('#vessel_sub_category_id').append("<option value=" + key + " "if(key == <?php echo $select_update['vessel_sub_category_id']; ?>){'selected';}" >" + value + "</option>");
Upvotes: 1
Views: 44
Reputation: 3709
Yes it is. But you need to put quotes around string you echo in PHP :)
$('#vessel_sub_category_id').append('<option value="' + key + '"' + if(key == '<?php echo $select_update['vessel_category_id']; ?>'){' selected';}+' >' + value + '</option>');
There would even be a smarter way to achieve this:
It would look then like this:
$('#vessel_sub_category_id').append('<option value="' + key + '"'+ (key == '<?=$select_update['vessel_category_id'];?>'?' selected':'')+' >' + value + '</option>');
Update: forgot the +
before the if and afterwards
Upvotes: 3