Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Nov 2010
    Location
    Washington DC
    Posts
    338
    Thanks
    22
    Thanked 1 Time in 1 Post

    Need assistance with adding additional table rows --ajax

    Hi:

    I was wondering if it is possible for an click() element for jquery to call another document.ready() element from within. Here is what I mean:
    I have:



    PHP Code:
     $('#addrow').click(function(){
                $('.item-row:last').after('<tr class="item-row"><td valign="top" align=center style="border-left: 0px solid #cccccc;" width="100%" class="item-name"><div class="delete-wpr"><?php echo $dd2;?><a class="delete" href="javascript:;" title="Remove row">D</a></div></td><td valign="top" align=center ><select id="service_subcat_1" style="color:#003399; text-align:justify; font-size:1.0em; border-left: none; border-right: none; border-bottom: none"><option>Select Category</option></select></td><td valign="top" align=center><div id="invtbox_" name="invtbox_">---</div></td><td><textarea id="descbox_" name="descbox_" rows="8" cols="40" class="expand80-1000" style="color:#003399; text-align:justify; font-size:1.0em; border-left: none; border-right: none; border-bottom: none">Make A Service or Product Selection</textarea></td><td valign="top" align=center><input  type="text" id="qtybox_" name="qtybox_"  class="qty" size="3" maxlength="3" value="0" class="combo3" rel="code_id" title="" style="color:#003399; text-align:left; font-size:1.1em; border-left: none; border-right: none; border-bottom: none"></td><td valign=top><textarea id="costbox_" name="costbox_" class="costbox"rows="1" cols="5" style="color:#003399; text-align:justify; font-size:1.0em; border-left: none; border-right: none; border-bottom: none"></textarea></td><input type="hidden" name="dimension2_id" value="0"><td valign="top" align=center style="border-left: 0px solid #cccccc;" width="14%" align="left"> <span class="price" id="pricebox_" name="pricebox_"  style="color:#003399; text-align:left; font-size:1.4em; border-left: none; border-right: none; border-bottom: none, border-top: 2px gray">$0</span></td></td></tr>');
                if ($('.delete').length > 0) { $('.delete').show(); }
                bind();
            });
    the select box id (id="service_subcat_1") within the $(.item-row:last).after()) is dependent on the following js to load addition list items:




    PHP Code:
      <script type="text/javascript">
        // ajax for multiple dropdown boxes
        $(document).ready(function() {
            var data_1 = <?php echo $arr1;?>;
            var id_1;
            function getData_1(pass_CatID){
                  var content_1 = '';
                var content = '<option name="specify id="specify" style="background: url() right no-repeat; width: 20px">------SPECIFY-----</option>';
                  var product_desc_ = '';
                var invt_ = '';
                var qty_ = '';
                var cost_ = '';
                id_1 = pass_CatID;
                $.each(data_1[id_1], function(key,value) {          
                    if(invt_ == '')invt_ = value['invt_'];
                    if(product_desc_ == '')product_desc_ = value['product_desc_'];
                    if(cost_ == '')cost_ = value['cost_'];
                     content_1 += '<option value="' + key + '">' + value['subcat_label_1'] + '</option>';
                   
                });
                $('#service_subcat_1').html(content_1);
                $('#descbox_').html( product_desc_ );
                $('#invtbox_').html(invt_);
                $('#qtybox_').html( qty_);
                $('#costbox_').html(cost_);
            }
            function getDesc_1(pass_ItemID){
                var item_ID = pass_ItemID;
                var invt_ = data_1[id_1][item_ID]['invt_'];
                $('#invtbox_').html(invt_);  
                
                 var product_desc_ = data_1[id_1][item_ID]['product_desc_'];
                $('#descbox_').html( product_desc_ ); 
                
                var qty_ = data_1[id_1][item_ID]['qty_'];
                $('#qtybox_').html( qty_ );   
                
                var cost_ = data_1[id_1][item_ID]['cost_'];
                $('#costbox_').html( cost_ );      
            }
            $('#categories_1').change(function(){
               id_1 = $('#categories_1').val();
               getData_1(id_1);
            });
            $('#service_subcat_1').change(function(){
               var item_ID = $('#service_subcat_1').val();
               getDesc_1(item_ID);
            });
        });
        </script>
    With the current structure, I am unable to achieve the result. May I ask for some thoughts on this?

    Mossa

  • #2
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,764
    Thanks
    19
    Thanked 155 Times in 146 Posts
    If you need something inside document.ready at times other than when the DOM is done loading, then you need to refactor the relevant code inside document.ready into a function, and then call the function inside document.ready as well as in your click event handler. I.e.:

    Code:
    function someFunction()
    {
         // some code
    }
    
    $(document).ready(function(){
      someFunction();
    });
    
    $("#element").click(function(){
      someFunction();
    });
    Regards, R.J.

    ---------------------------------------------------------

    Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :)
    [Related videos and tutorials are also available at my YouTube channel and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!

  • #3
    Regular Coder
    Join Date
    Nov 2010
    Location
    Washington DC
    Posts
    338
    Thanks
    22
    Thanked 1 Time in 1 Post
    thanks for your reply! would my code look like this --as it pertains to your reply?

    PHP Code:
    function populateBox()
    {
        var data_1 = {"2":{"25":{"subcat_label_":"Anti-Lock Systems ","invt_":"Ok","product_desc_":"","qty_":"0","cost_":"0.00"},"21":{"subcat_label_":"Brake Drums ","invt_":"Ok","product_desc_":"","qty_":"0","cost_":"0.00"},"22":{"subcat_label_":"Brake Rotors ","invt_":"Ok","product_desc_":"","qty_":"0","cost_":"0.00"},"20":{"subcat_label_":"Emergency Brakes ","invt_":"Ok","product_desc_":"","qty_":"0","cost_":"0.00"},"18":{"subcat_label_":"Hose Replacement ","invt_":"Ok","product_desc_":"","qty_":"0","cost_":"0.00"},"16":{"subcat_label_":"Lining Replacement ","invt_":"Ok","product_desc_":"","qty_":"0","cost_":"0.00"},"23":{"subcat_label_":"Machining ","invt_":"Ok","product_desc_":"","qty_":"0","cost_":"0.00"},"4":{"subcat_label_":"Master Cylinders","invt_":"2","product_desc_":"","qty_":"0","cost_":""},"17":{"subcat_label_":"Pad Replacement ","invt_":"Ok","product_desc_":"","qty_":"0","cost_":"0.00"},"24":{"subcat_label_":"Power Boosters ","invt_":"Ok","product_desc_":"","qty_":"0","cost_":"0.00"},"19":{"subcat_label_":"Wheel Cylinders ","invt_":"Ok","product_desc_":"","qty_":"0","cost_":"0.00"}},"1":{"13":{"subcat_label_":"Battery Service ","invt_":"Ok","product_desc_":"New Battery Installed","qty_":"0","cost_":"0.00"},"10":{"subcat_label_":"Brake Fluid Service ","invt_":"Ok","product_desc_":"","qty_":"0","cost_":"0.00"},"5":{"subcat_label_":"Chassis Lube","invt_":"Ok","product_desc_":"Injected grease between moving joints and into zerks (grease fittings).","qty_":"0","cost_":"0.00"},"15":{"subcat_label_":"Cooling System Service ","invt_":"Ok","product_desc_":"Drain, flush and refill the cooling system.  Inspected all hoses.  Cleaned the radiator, condenser, pressure cap and neck.  Finally, pressure tested the cooling system and pressure cap.","qty_":"0","cost_":"0.00"},"8":{"subcat_label_":"Differential Service ","invt_":"Ok","product_desc_":"","qty_":"0","cost_":"0.00"},"14":{"subcat_label_":"Noises & Leaks ","invt_":"Ok","product_desc_":"","qty_":"0","cost_":"0.00"},"3":{"subcat_label_":"Oil & Filter Changes","invt_":"43","product_desc_":"Drained old oil and add new clean oil to properly lubricate and cool the moving parts of the engine, as well as to prevent corrosion and rust. A new oil filter installed to help strain out debris and small particles from the oil.","qty_":"0","cost_":""},"7":{"subcat_label_":"Pack Wheel Bearings ","invt_":"Ok","product_desc_":"","qty_":"0","cost_":"0.00"},"9":{"subcat_label_":"Power Steering Service ","invt_":"Ok","product_desc_":"","qty_":"0","cost_":"0.00"},"6":{"subcat_label_":"Transmission Service ","invt_":"Ok","product_desc_":"","qty_":"0","cost_":"0.00"},"12":{"subcat_label_":"Windshield Washers ","invt_":"10","product_desc_":"","qty_":"0","cost_":"10.99"},"11":{"subcat_label_":"Wiper Blades ","invt_":"47","product_desc_":"","qty_":"0","cost_":"2.45"}},"4":{"36":{"subcat_label_":"A\/C Service ","invt_":"Ok","product_desc_":"","qty_":"0","cost_":"0.00"},"41":{"subcat_label_":"A\/C System Repair ","invt_":"Ok","product_desc_":"","qty_":"0","cost_":"0.00"},"40":{"subcat_label_":"A\/C-Heater Controls ","invt_":"Ok","product_desc_":"","qty_":"0","cost_":"0.00"},"38":{"subcat_label_":"Blower Motors ","invt_":"Ok","product_desc_":"","qty_":"0","cost_":"0.00"},"35":{"subcat_label_":"CC-Diagnosis ","invt_":"Ok","product_desc_":"","qty_":"0","cost_":"0.00"},"39":{"subcat_label_":"Heater Cores ","invt_":"Ok","product_desc_":"","qty_":"0","cost_":"0.00"},"42":{"subcat_label_":"Heating System Repair ","invt_":"Ok","product_desc_":"","qty_":"0","cost_":"0.00"},"37":{"subcat_label_":"R-12 & R134a Systems ","invt_":"Ok","product_desc_":"","qty_":"0","cost_":"0.00"}},"3":{"28":{"subcat_label_":"Belts ","invt_":"Ok","product_desc_":"","qty_":"0","cost_":"0.00"},"26":{"subcat_label_":"Diagnosis ","invt_":"Ok","product_desc_":"","qty_":"0","cost_":"0.00"},"32":{"subcat_label_":"Electric Fans ","invt_":"Ok","product_desc_":"","qty_":"0","cost_":"0.00"},"31":{"subcat_label_":"Fan Clutches ","invt_":"Ok","product_desc_":"","qty_":"0","cost_":"0.00"},"29":{"subcat_label_":"Heater Cores ","invt_":"Ok","product_desc_":"","qty_":"0","cost_":"0.00"},"27":{"subcat_label_":"Hoses ","invt_":"12","product_desc_":"","qty_":"0","cost_":"0.00"},"30":{"subcat_label_":"Thermostats ","invt_":"Ok","product_desc_":"","qty_":"0","cost_":"0.00"},"34":{"subcat_label_":"Transmission Coolers ","invt_":"Ok","product_desc_":"","qty_":"0","cost_":"0.00"},"33":{"subcat_label_":"Water Pumps ","invt_":"2","product_desc_":"","qty_":"0","cost_":"0.00"}}};
    //var data_1 = <?php echo $arr1;?>;
        var id_1;
        function getData_1(pass_CatID){
              var content_1 = '';
            var content = '<option name="specify id="specify" style="background: url() right no-repeat; width: 20px">------SPECIFY-----</option>';
              var product_desc_ = '';
            var invt_ = '';
            var qty_ = '';
            var cost_ = '';
            id_1 = pass_CatID;
            $.each(data_1[id_1], function(key,value) {          
                if(invt_ == '')invt_ = value['invt_'];
                if(product_desc_ == '')product_desc_ = value['product_desc_'];
                if(cost_ == '')cost_ = value['cost_'];
                 content_1 += '<option value="' + key + '">' + value['subcat_label_'] + '</option>';
               
            });
            $('#service_subcat_').html(content_1);
            $('#descbox_').html( product_desc_ );
            $('#invtbox_').html(invt_);
            $('#qtybox_').html( qty_);
            $('#costbox_').html(cost_);
        }
        function getDesc_1(pass_ItemID){
            var item_ID = pass_ItemID;
            var invt_ = data_1[id_1][item_ID]['invt_'];
            $('#invtbox_').html(invt_);  
            
             var product_desc_ = data_1[id_1][item_ID]['product_desc_'];
            $('#descbox_').html( product_desc_ ); 
            
            var qty_ = data_1[id_1][item_ID]['qty_'];
            $('#qtybox_').html( qty_ );   
            
            var cost_ = data_1[id_1][item_ID]['cost_'];
            $('#costbox_').html( cost_ );      
        }
    }
      $(document).ready(function(){
      populateBox();
    });
          $('#categories_1').change(function(){
           id_1 = $('#categories_1').val();
           getData_1(id_1);
             populateBox();

        });
        $('#service_subcat_1').change(function(){
           var item_ID = $('#service_subcat_1').val();
           getDesc_1(item_ID);
             populateBox();

        });
    or do I have the wrong understanding?
    Last edited by mbarandao; 05-27-2012 at 10:31 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •