0

Today I am working with select controller, I have tried different ways to set the default value as the first one but can't. For this stuff, I tried below code.

Array

$colorProductId = array('2' => 'Black','3' => 'Blue');
<span class="select_span">Select Color: </span>
<select id="color_drop_down_options" data-role="color_drop_down_options" class="color_drop_down_options" onChange="check();">
   <?php foreach($colorProductId as $key => $value): ?>
        <option value="<?php /* @escapeNotVerified */ echo $value ?>"<?php if ($value): ?> selected="selected"<?php endif ?>>
            <?php /* @escapeNotVerified */ echo $key ?>
        </option>
   <?php endforeach; ?>
 </select>

Above code is selected last one see image.

enter image description here

please suggest me how to set first one as default selected.

2 Answers 2

0

Finally solved my self.

<script type="text/javascript">
    require(['jquery'],function($){
        $(document).ready(function() {
            $("#color_drop_down_options > [value='<?php echo $FirstIndex ?>']").attr("selected", "true");
        });
    });
</script>
0

You should have been able to do it in php

<?php if ($value == $FirstIndex){ ?> selected="selected" <?php } ?>

No need for jquery hack.

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.