1

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>"); 

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

2 Comments

sir marc still not displaying the selected value.
Update: There was a problem with your quoting: Replaced the double quotes with single to make sure the value has double quotes

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.