0

I am rendering all the option of the select box through an ajax call inside $(document).ready() function.

I have a PHP variable at the top of the page which is initialized with 1 like

$expert_id  = 1;

Now here is my code

$(document).ready(function(){
        $.ajax({
            type: "POST",
            url: "<?php echo base_url('insoadmin/experts/all_experts');?>",
            dataType: "json",            
            success: function(data, page) {
                $.each(data, function(i, item) {
                    //console.log(data[i].expert_name);
                    $('#expert').append($('<option>', { 
                        value: data[i].expert_id,
                        text : data[i].expert_name,
                    }));
                });
            }
        });
    });

While appending all the option to my expert select box I want to check if data[i].expert_id is equal to my php variable or not, if it is equal to my PHP variable then it should be selected.

How can I achieve that?

3 Answers 3

1

Use selected property/attribute while creating jQuery object(<option>)

Refer jQuery( html [, ownerDocument ] )

var data = [{
  expert_id: 1,
  expert_name: 'rayon'
}, {
  expert_id: 2,
  expert_name: 'bye'
}];
var expert_id = 2;
$.each(data, function(i, item) {
  $('#expert').append($('<option>', {
    value: data[i].expert_id,
    text: data[i].expert_name,
    selected: expert_id === data[i].expert_id
  }));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="" id="expert"></select>

Fiddle Demo

4
  • selected: expert_id === data[i].expert_id : i want this logic with the PHP variable Aug 22, 2016 at 11:16
  • 1
    @PunitGajjar – You can have a JavaScript variable holding PHP-value, ex. var expert_id = <?php $expert_id; ?>
    – Rayon
    Aug 22, 2016 at 11:17
  • Thanks mate, it works like charm, you have made my day . . . Thank you so much Aug 22, 2016 at 11:20
  • @PunitGajjar – Happy to help! Kindly accept and up-vote the best solution which has solved the purpose :) Happy coding!
    – Rayon
    Aug 22, 2016 at 11:21
1

Have you tried like this:

$(document).ready(function(){
        $.ajax({
            type: "POST",
            url: "<?php echo base_url('insoadmin/experts/all_experts');?>",
            dataType: "json",            
            success: function(data, page) {
                $.each(data, function(i, item) {
                    //console.log(data[i].expert_name);

                    $('#expert').append($('<option>', { 
                        value: data[i].expert_id,
                        text : data[i].expert_name,
                    }));
                });

                var expert_id = '<?php echo $expert_id ; ?>';
                $('#expert').val(expert_id); //setting the value of select box
            }
        });
    });
3
  • yeah this can be the way , but i would like to check it within loop appreciate your answer Aug 22, 2016 at 11:22
  • its a one time activity. So its better to do it once all the option gets appended. Checking the value within loop will obviously will add few mill seconds.
    – vijayP
    Aug 22, 2016 at 11:24
  • This was just an example , actually i have dynamic select boxes Aug 22, 2016 at 11:29
0

Define at top:

<span id="foo"><?php echo $expert_id; ?></span>

Add in the script

<script>


 $(document).ready(function(){
            $.ajax({
                type: "POST",
                url: "<?php echo base_url('insoadmin/experts/all_experts');?>",
                dataType: "json",            
                success: function(data, page) {
                    $.each(data, function(i, item) {
                        //console.log(data[i].expert_name);
                        $('#expert').append($('<option>', { 
                            value: data[i].expert_id,
                            text : data[i].expert_name,
              var foo=$('#foo').html();
              if(foo==data[i].expert_id){
          //do something
           }
                        }));
                    });
                }
            });
        });
    </script>

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.