0

Can someone point out to me, on what could be done different in this code, so the On('click') event can be bound to the newly inserted link. Right now there is a link which gets replaced at the end of the function. But after it has been replaced, it doesn't have ON('clicked') attached to it any more. Any suggestions greatly appreciated.

(function(){
    var form, $this, info, modal;
    modal=$('#addPreviousCrop');
    form=modal.find('form').first();
    $("a[data-target='#addPreviousCrop']").on('click',function(){
        modal.find('.panel-heading h4').first().text('Update Previous Crop');
        $this=$(this);
        if($this.data('method')==='PATCH'){
            //get the record to be updated
            resetForm(form);
            $.get('dairyCrops/'+$this.data('id')+'/edit',function(data){
            populateForm(form,data,data.id);
        });
        }
        form.find('#company_field')
                .html(' <strong>'+$this.data('company_name')+' - '+$this.data('field')+'</strong>');
        form.find('input[name=acreage]').val($this.data('acreage'));
        form.find('select[name=dairy_crop_variety_id]').on('change',function(){
            var selVal=this.options[this.selectedIndex].text;
            form.find('input[name=seeding_rate]').val(getSeedingRate(selVal));
        });
        info = {property_id:$this.data('property_id'),status:'previous'};
    });

     //submit previous crop
    form.on('submit',function(e){
        e.preventDefault();
        $.when(submitAjaxRequest(this,info)).then(function(data, textStatus, jqXHR){
            modal.modal('hide');
            $this.closest('td').html(data.crop).slideDown('slow');
        });
    });
})();
Barmar
  • 741,623
  • 53
  • 500
  • 612
Guntar
  • 473
  • 8
  • 23

0 Answers0