0

I have a codeigniter3 project.

My js-file with a footer include:

<script src="<?= base_url() ?>assets/myjsfile.js"></script>

In the normal codeigniter views, I can work with the file and the functions from it.

If I open a modal and load a new view by an ajax call over the controller, I couldn't work with this js-file. I always receive the error

"Uncaught ReferenceError: myfunctionname is not defined"

in my view, I add a modal-template

   <div class="modal fade" id="modal_default" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
          <div class="modal-dialog modal-lg">
              <div class="modal-content"></div>
                </div>
          </div>
      </div>

I load a viewpage onclick into this modal by this controller function

public function_load_content(){ 
        $this->load->view('mycontentfile',$this->input->post());        
    }

The modal will show and the content also loads correctly.

In this context, I try to call a function on click, which is defined in the "myjsfile.js"

$(document).ready(function(e){
    $(document).on('click', '#mybutton', function(){
        myfunctionname();
    });   
});

I tried a lot. Add it into document.ready, move it outside document.ready, add jquery & bootstrap library into the view-file...all my tries failed.

Why i can't access the myfunctionsname()?

In "myjsfile.js" myfunctionname is defined as follows

$(document).ready(function(e){

function myfunctionname(){...}

});

Working on a normal view file which is not loaded later by a ajaxcall runs great.

What do I have to do to make this work and access the file/functions the same way as a normal view?

FSDford
  • 448
  • 5
  • 10
relo80
  • 245
  • 2
  • 13
  • https://stackoverflow.com/questions/60289506/capture-the-click-event-of-dynamic-buttons-in-bootstrap-modal-using-jquery – FSDford Dec 31 '20 at 10:37
  • HI @FSDford, how i can use this post for my sutiation of a function, did i have to change the "onclick" part inside the modal? My problem is not the onclick event, it works, but the function is not avivable – relo80 Dec 31 '20 at 10:53
  • Please try to make a runnable snippet. What version of jQuery are you using? – FSDford Dec 31 '20 at 10:56
  • i use jquery 3.5.1 – relo80 Dec 31 '20 at 11:13
  • i trie to make a snippets, but it failed, without codeigniter-framework i could not make a snippet wich has same situation as at my side – relo80 Dec 31 '20 at 11:23
  • Have you ever used console.log to debug a problem like this? Put one in the on click event listener to see if it even fires. I don’t think codeigniter is relevant to the solution and it is a distraction to jQuery and JavaScript developers who can help you if you post a minimal reproductable example. Right click your HTML source code and copy / paste that here to a stack snippet and go from there. – FSDford Jan 04 '21 at 08:22

0 Answers0