0

I have a field with selection options in the php file:

<div class="form-group">
    <div class="col-sm-12">
        <div class="input-group">
            <div class="input-group-addon"><?php _e('Taxonomy', 'textdomain'); ?></div>
            <div class="select">
                <select name="categoryxpath_tax" id="select-taxonomy-type" class="form-control" ng-model="model.categoryxpath_tax">
                    <option value=""><?php _e('Please select a taxonomy', 'textdomain'); ?></option>
                    <?php
                    if (get_post_meta($post_object->ID, 'sc_post_type', true) == "") {
                        $taxonomies = get_object_taxonomies('post', 'objects');
                    } else {
                        $taxonomies = get_object_taxonomies(get_post_meta($post_object->ID, 'sc_post_type', true), 'objects');
                    }
                    foreach ($taxonomies as $taxonomy) { ?>
                    <option value="<?php echo $taxonomy->name; ?>"><?php echo $taxonomy->labels->name; ?></option>
                    <?php } ?>
                </select>
            </div>
        </div>  
    </div>
</div>

And I created the same form in JavaScript and I want to get the values of the foreach loop. But I can't get the values in JavaScript in the form.

if (type == 'taxonomy_field') {
    var taxonomy_value = document.getElementById('select-taxonomy-type');
    var taxonomy_field_name = taxonomy_value.options[taxonomy_value.selectedIndex].text;
    var taxonomy_field_value = taxonomy_value.options[taxonomy_value.selectedIndex].value;
    $($event.target).closest('.form-group').before($compile(
        '<div class="form-group">' +
            '<div class="col-sm-12">' +                         
                '<div class="input-group">' +
                    '<div class="input-group-addon">' + translate.Taxonomy + '</div>' +
                    '<div class="select">' +
                        '<select name="categoryxpath_tax" class="form-control">' +
                            '<option value="">' + translate.Please_select_a_taxonomy + '</option>' +
                            '<option value="' + taxonomy_field_value + '">' + taxonomy_field_name + '</option>' +
                        '</select>' +   
                    '</div>' +  
                    '<span class="input-group-btn"><button type="button" class="btn btn-primary btn-block" ng-click="remove_field($event)"><i class="icon ion-trash-a"></i></button></span>' +
                '</div>' +
            '</div>' +  
        '</div>'
    )($scope));
}

How do I get foreach loop values in JavaScript? Thank you if Put the edited JavaScript code for me.

mohsen
  • 11
  • 1

1 Answers1

0

Check out this post. You can use json_encode to pass the array and then JavaScript forEach that. I think it should go something like this.

var array = <?php echo json_encode($taxonomies); ?> ;
array.forEach(function (item) {
 '<option value="">' + translate.Please_select_a_taxonomy + '</option>' +
 '<option value="' + item.value + '">' + item.name + '</option>' +
                });    

There will be some bugs here, but that should give you a good start

  • Thankful. But I want to get the values in the JS file, not the php file. How do I get the values in the JS file? – mohsen May 02 '20 at 13:40