2

Magento 2 How to remove --data-mage-init='{"validation":{}}-- when div is hidden, I hide div when select box value is not equal to 0 using Script

<div class="row">
    <div class="col-md-12" style="margin-top:30px;">
        <form name="addData" method="post" id="addData" class="form" action="<?php echo $this->getFormAction(); ?>" data-hasrequired="<?= $block->escapeHtmlAttr(__('* Required Fields')) ?>" data-mage-init='{"validation":{}}'>
        <?php foreach($quote->getAllVisibleItems() as $_item) { 
            $product = $objectManager->create('Magento\Catalog\Model\Product')->load($_item->getProductId());
            //$categoryFactory = $objectManager->get('\Magento\Catalog\Model\CategoryFactory'); 
            $categories = $product->getCategoryIds();
            
            $product_ID = $product->getId();
            
            foreach($categories as $category){
                $cat = $objectManager->create('Magento\Catalog\Model\Category')->load($category);
                $cat_id = $cat->getId();
                //echo $cat_id;                 
                
            /* $category = $_objectManager->create('Magento\Catalog\Model\Category')->load($categoryId);
                echo $category->getName(); */
            
            $customerId = $block->getLoggedinCustomerId();
            //$product->getResource()->getAttribute('rx')->getFrontend()->getValue($product);
            //echo $product->getRx(); die();
            $is_rx = $product->getData('rx');
            
            if($is_rx != "" && $is_rx == '5479'){
            if($cat_id == '27' || $cat_id == '29' || $cat_id == '30' || $cat_id == '53'){  // change prescription option id here ?>
            <div class="container">
                <div class="row">
            <fieldset class="fieldset col-sm-6">    
                <legend class="legend"><h3>Add Prescription: <?php echo $_item->getName(); ?></h3></legend>             
                <fieldset class="fieldset row">
                <div class="fields col-md-8">
                <?php
                    //$sql = $connection->select()->from(["tn" => $prescription_tbl]);
                $sql = "Select * FROM ". $prescription_tbl. " WHERE customer_id =". $customerId. " && product_id =". $product_ID;
                $result = $connection->fetchAll($sql);
                    
                    echo '<select id="test" name="form_select" onchange="showDiv(this)">
                    <option value="0">Please Select Option</option>';
                    
                    foreach ($result as $values) {
                        $name = $values['firstname'];
                        $id = addslashes($values['id']);
                        echo "<option value='$id'>$name</option>";
                    }
                    echo '</select>'        
                    ?>
                </div>
                    
                    <div class="fields col-md-8" id="hidden_div">   
                        <div class="field name required">
                            <label class="label" for="firstname"><span>First Name</span></label>
                            <div class="control">
                            <input name="firstname[]" id="firstname" title="F_Name" value="" class="input-text" type="text" data-validate="{required:true}">
                            </div>
                        </div>
                        <div class="field name required">
                            <label class="label" for="lastname"><span>Last Name</span></label>
                            <div class="control">
                            <input name="lastname[]" id="lastname" title="L_Name" value="" class="input-text" type="text" data-validate="{required:true}">
                            </div>
                        </div>
                        <div class="field required">
                            <label class="label" for="allergies"><span>Allergies</span></label>
                            <div class="control">
                            <input name="allergies[]" id="allergies" title="Allergies" value="" class="input-text" type="text" data-validate="{required:true}">
                            </div>
                        </div>
                        <div class="field required">
                            <label class="label" for="date_of_birth"><span>Date Of Birth</span></label>
                            <div class="control">
                                <input type="text" name="date_of_birth[]" title="DOB" id="datepicker" data-validate="{required:true}">  
                            </div>
                        </div>
                        <div class="field required">
                            <label class="label" for="address"><span>Address</span></label>
                            <div class="control">                               
                                <input name="address[]" id="address" title="Address" class="input-text" type="text" data-validate="{required:true}"></input>
                            </div>
                        </div>
                        <div class="field required">
                            <label class="label" for="phone_number"><span>Phone No.</span></label>
                            <div class="control">                               
                                <input name="phone_number[]" id="Phone" title="Phone" class="input-text" type="text" data-validate="{required:true}"></input>
                            </div>
                        </div>
                        <div class="field required">
                            <label class="label" for="delivery_method"><span>Delivery Method</span></label>
                            <select name="delivery_method[]">
                            <option>---Select Method---</option>
                            <option value="Delivery 1">Delivery 1</option>
                            <option value="Delivery 2">Delivery 2</option>
                            <option value="Delivery 3">Delivery 3</option>
                            <option value="Delivery 4">Delivery 4</option>
                            </select>
                        </div>
                    </div>
                </fieldset>
                <fieldset style="display:none">             
                    <div class="field required" style="display:none">
                    <label class="label" for="product_id"><span></span></label>
                        <select name="product_id[]">
                        <option value="<?php echo $_item->getProductId(); ?>"><?php echo $_item->getProductId(); ?></option>
                        </select>
                    </div>
                    <div class="field required" style="display:none">
                    <label class="label" for="customer_id"><span></span></label>
                        <select name="customer_id[]">
                        <option value="<?php echo $customerId; ?>"><?php echo $customerId; ?></option>
                        </select>
                    </div>
                </fieldset>
            </fieldset>
            <fieldset class="fieldset col-sm-6">
                <legend class="legend"><h3>Add Doctor Details:</h3></legend>                
                <fieldset class="fieldset row">
                <div class="fields col-md-8">
                
                    <?php
                    //$sql = $connection->select()->from(["tn" => $doctor_tbl]);
                    $sql = "Select * FROM ". $doctor_tbl. " WHERE customer_id =". $customerId. " && product_id =". $product_ID;
                    $result = $connection->fetchAll($sql);
                    
                    echo '<select id="getFname" onchange="admSelectCheck(this);">
                    <option id="admOption" value="0">Please Select Option</option>';
                    
                    foreach ($result as $values) {
                        $name = $values['firstname'];
                        $id = addslashes($values['id']);
                        echo "<option value='$id'>$name</option>";
                    }
                    echo '</select>';
                    ?>
                </div>
                    <div class="fields col-md-8" id="admDivCheck">
                        <div class="field name required">
                            <label class="label" for="d_firstname"><span>First Name</span></label>
                            <div class="control">
                            <input name="d_firstname[]" id="d_firstname" title="DF_Name" value="" class="input-text" type="text" data-validate="{required:true}">
                            </div>
                        </div>
                        <div class="field name required">
                            <label class="label" for="d_lastname"><span>Last Name</span></label>
                            <div class="control">
                            <input name="d_lastname[]" id="d_lastname" title="DL_Name" value="" class="input-text" type="text" data-validate="{required:true}">
                            </div>
                        </div>
                        <div class="field required">
                            <label class="label" for="hospital"><span>Hospital</span></label>
                            <div class="control">
                            <input name="hospital[]" id="hospital" title="Hospital" value="" class="input-text" type="text" data-validate="{required:true}">
                            </div>
                        </div>
                        <div class="field required">
                            <label class="label" for="d_address"><span>Address</span></label>
                            <div class="control">                               
                                <input name="d_address[]" id="d_address" title="D_Address" class="input-text" type="text" data-validate="{required:true}"></input>
                            </div>
                        </div>
                        <div class="field required">
                            <label class="label" for="specialization"><span>Specialization</span></label>
                            <div class="control">                               
                                <input name="specialization[]" id="specialization" title="Specialization" class="input-text" type="text" data-validate="{required:true}"></input>
                            </div>
                        </div>  
                    </div>
                </fieldset>             
            </fieldset>
                </div>
            </div>
            
        <?php } 
        else if ($cat_id == '58') { // Veterinary Form?>
                <div class="container">
                <div class="row">
            <fieldset class="fieldset col-sm-6">    
                <legend class="legend"><h3>Add Prescription: <?php echo $_item->getName(); ?></h3></legend>             
                <fieldset class="fieldset row">
                    <div class="fields col-md-8">
                    
                    <?php
                    //$sql = $connection->select()->from(["tn" => $veterinary_tbl]);
                    $sql = "Select * FROM ". $veterinary_tbl. " WHERE customer_id =". $customerId. " && product_id =". $product_ID;
                    $result = $connection->fetchAll($sql);
                    
                    echo '<select id="getLname" onchange="SelectCheck(this);">
                    <option id="Option" value="0">Please Select Option</option>';
                    
                    foreach ($result as $values) {
                        $name = $values['firstname'];
                        $id = addslashes($values['id']);
                        echo "<option value='$id'>$name</option>";
                    }
                    echo '</select>'        
                    ?>
                    
                    </div>
                    <div class="fields col-md-8" id="DivCheck">
                        <div class="field name required">
                            <label class="label" for="v_firstname"><span>Name</span></label>
                            <div class="control">
                            <input name="v_firstname[]" id="v_firstname" title="F_Name" value="" class="input-text" type="text" data-validate="{required:true}">
                            </div>
                        </div>
                        <div class="field required">
                            <label class="label" for="v_allergies"><span>Allergies</span></label>
                            <div class="control">
                            <input name="v_allergies[]" id="v_allergies" title="Allergies" value="" class="input-text" type="text" data-validate="{required:true}">
                            </div>
                        </div>
                        <div class="field required">
                            <label class="label" for="v_date_of_birth"><span>Date Of Birth</span></label>
                            <div class="control">
                                <input type="text" name="date_of_birth[]" title="DOB" id="datepicker" data-validate="{required:true}">      
                            </div>
                        </div>
                        <div class="field required">
                            <label class="label" for="breed"><span>Breed</span></label>
                            <div class="control">                               
                                <input name="breed[]" id="breed" title="Breed" class="input-text" type="text" data-validate="{required:true}"></input>
                            </div>
                        </div>
                        <div class="field required">
                            <label class="label" for="v_address"><span>Address</span></label>
                            <div class="control">                               
                                <input name="v_address[]" id="v_address" title="Address" class="input-text" type="text" data-validate="{required:true}"></input>
                            </div>
                        </div>
                        <div class="field required">
                            <label class="label" for="v_phone_number"><span>Phone No.</span></label>
                            <div class="control">                               
                                <input name="v_phone_number[]" id="v_Phone" title="Phone" class="input-text" type="text" data-validate="{required:true}"></input>
                            </div>
                        </div>
                        <div class="field required">
                            <label class="label" for="v_delivery_method"><span>Delivery Method</span></label>
                            <select name="v_delivery_method[]">
                            <option>---Select Method---</option>
                            <option value="Delivery 1">Delivery 1</option>
                            <option value="Delivery 2">Delivery 2</option>
                            <option value="Delivery 3">Delivery 3</option>
                            <option value="Delivery 4">Delivery 4</option>
                            </select>
                        </div>              
                    </div>
                </fieldset>
                <fieldset style="display:none">             
                    <div class="field required" style="display:none">
                    <label class="label" for="v_product_id"><span></span></label>
                        <select name="v_product_id[]">
                        <option value="<?php echo $_item->getProductId(); ?>"><?php echo $_item->getProductId(); ?></option>
                        </select>
                    </div>
                    <div class="field required" style="display:none">
                    <label class="label" for="v_customer_id"><span></span></label>
                        <select name="v_customer_id[]">
                        <option value="<?php echo $customerId; ?>"><?php echo $customerId; ?></option>
                        </select>
                    </div>
                </fieldset>
            </fieldset>
            <fieldset class="fieldset col-sm-6">
                <legend class="legend"><h3>Add Doctor Details:</h3></legend>
                <fieldset class="fieldset row">
                    <div class="fields col-md-8">
                    
                    <?php
                    //$sql = $connection->select()->from(["tn" => $doctor_tbl]);
                    $sql = "Select * FROM ". $doctor_tbl. " WHERE customer_id =". $customerId. " && product_id =". $product_ID;
                    $result = $connection->fetchAll($sql);
                    
                    echo '<select id="getFname" onchange="selectCheck(this);">
                    <option id="dmOption"  value="0">Please Select Option</option>';
                    
                    foreach ($result as $values) {
                        $name = $values['firstname'];
                        $id = addslashes($values['id']);
                        echo "<option id='0' value='$id'>$name</option>";
                    }
                    echo '</select>';
                    ?>
                    </div>
                    
                    <div class="fields col-md-8" id="dmDivCheck">                   
                        <div class="field name required">
                            <label class="label" for="d_firstname"><span>First Name</span></label>
                            <div class="control">
                            <input name="d_firstname[]" id="d_firstname" title="DF_Name" value="" class="input-text" type="text" data-validate="{required:true}">
                            </div>
                        </div>
                        <div class="field name required">
                            <label class="label" for="d_lastname"><span>Last Name</span></label>
                            <div class="control">
                            <input name="d_lastname[]" id="d_lastname" title="DL_Name" value="" class="input-text" type="text" data-validate="{required:true}">
                            </div>
                        </div>
                        <div class="field required">
                            <label class="label" for="hospital"><span>Hospital</span></label>
                            <div class="control">
                            <input name="hospital[]" id="hospital" title="Hospital" value="" class="input-text" type="text" data-validate="{required:true}">
                            </div>
                        </div>
                        <div class="field required">
                            <label class="label" for="d_address"><span>Address</span></label>
                            <div class="control">                               
                                <input name="d_address[]" id="d_address" title="D_Address" class="input-text" type="text" data-validate="{required:true}"></input>
                            </div>
                        </div>
                        <div class="field required">
                            <label class="label" for="specialization"><span>Specialization</span></label>
                            <div class="control">                               
                                <input name="specialization[]" id="specialization" title="Specialization" class="input-text" type="text" data-validate="{required:true}"></input>
                            </div>
                        </div>  
                    </div>
                </fieldset>             
            </fieldset>
                </div>
            </div>      
        
        
        <?php } } } } ?>
            
        <div class="actions-toolbar">
            <div class="primary">
                <button type="submit" class="action submit primary" title="Save"><span>Save</span></button>
            </div>
        </div>
        </form>
    </div>
</div>
<script type="text/javascript">
    function admSelectCheck(nameSelect)
{
    console.log(nameSelect);
    if(nameSelect){
        admOptionValue = document.getElementById("admOption").value;
        if(admOptionValue == nameSelect.value){
            document.getElementById("admDivCheck").style.display = "block";
        }
        else{
            document.getElementById("admDivCheck").style.display = "none";
        }
    }
    else{
        document.getElementById("admDivCheck").style.display = "none";
    }
}
</script>

0

Your Answer

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

Browse other questions tagged or ask your own question.