JavaScript Give Default Checked Value To The Checkbox Using Jquery

I tried to add checked property to a specific checkbox using jquery, but it seems doesn't work properly.

Checkbox Function Display

function product_category_loop_array ($product_category_array = array (), $parent_id = 0, $margin = -20) {
            if (!empty ($product_category_array[$parent_id])) {
                $margin = $margin + 20;
                foreach ($product_category_array[$parent_id] as $items) {
                    echo "<input style='margin: 0 0 0 ".$margin."px; overflow: hidden;' id='".$items['id']."' class='category_checkbox left' name='product_category[]' type='checkbox' value='".$items['id']."'><label class='category_checkbox_label left'>".$items['name']."</label>
                    <div class='clear'></div>";
                    product_category_loop_array ($product_category_array, $items["id"], $margin);
                }
            }
        }
        function product_category () {
            $db_connect = mysqli_connect (db_host, db_username, db_password, db_name);
            $product_category_query = $db_connect->query ("SELECT id, name, parent_id FROM `product_category` ORDER BY name ASC");
            $product_category_array = array ();
            if (mysqli_num_rows ($product_category_query)) {
                while ($row = mysqli_fetch_array ($product_category_query, MYSQLI_ASSOC)) {
                    $product_category_array[$row['parent_id']][] = $row;
                }
                product_category_loop_array ($product_category_array);
            }
        }

HTML

<div id="tabs-2" class="product_detail hide">
                            <div class="left">
                                <form class="product_detail_form" method="post" action="">
                                    <?php product_category () ?>
                                    <p class="detail_submit"><input name="save_product_category" type="submit" value="Save"></p>
                                </form>
                            </div>
                        </div>

Jquery

<script>
    $ (document) .ready (function () {
        <?php echo $hide_div ?>;
        var selected_array = <?php echo json_encode ($product_category_selected) ?>;
        $.each (selected_array (key, value) {
            $ ("#"+value).prop("checked", true);
        });
    });
</script>

Any help will be appreciated.

Answer:1

Use

$ ("#"+value).attr("checked", "checked");
Answer:2

EDIT: This is for an Electron project, with a local server spun up on the user's system. So, any concerns about what happens if multiple users attempt simultaneous access can be ignored! My client ...

EDIT: This is for an Electron project, with a local server spun up on the user's system. So, any concerns about what happens if multiple users attempt simultaneous access can be ignored! My client ...

  1. javascript resolve promises sequentially
  2. functional javascript resolving promises sequentially
  3. resolve promises sequential

I´m using mongoose and I need to find a model name from a model instance. In one part of the code I have: const schema = new mongoose.Schema({ name: { type: String, ...

I´m using mongoose and I need to find a model name from a model instance. In one part of the code I have: const schema = new mongoose.Schema({ name: { type: String, ...

  1. there she goes model name
  2. there she goes song model name

Im using codeigniter to load a view. This view has a button to open a modal and in this modal i must load a method from another controller. To do this, i'm using ajax passing data by POST. This is my ...

Im using codeigniter to load a view. This view has a button to open a modal and in this modal i must load a method from another controller. To do this, i'm using ajax passing data by POST. This is my ...

  1. jquery ajax cross domain
  2. jquery.ajax-cross-origin.min.js
  3. jquery ajax cross domain access-control-allow-origin
  4. jquery ajax cross origin
  5. jquery ajax cross-origin request blocked
  6. jquery ajax cross domain post
  7. jquery ajax cross domain request example
  8. jquery ajax redirect to another page
  9. jquery ajax redirect to another page with data
  10. jquery ajax cross domain example

I'm trying to sort the JSON by release date, by random, by alphabetical order or by popularity. I'm not sure if the answer is on here, I checked and I can't seem to figure out if it's for me. My ...

I'm trying to sort the JSON by release date, by random, by alphabetical order or by popularity. I'm not sure if the answer is on here, I checked and I can't seem to figure out if it's for me. My ...