1

My goal is to pass a jQuery script through a model with CodeIgneter.

This is the view where I linked the jquery.js file

  <footer>
   <p>&copy; Company 2014</p>
  </footer>      
  <script src="<?php echo base_url('assets/js/jquery.js');?>">  </script>

   </div> <!-- /container -->

This is the model jquery

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Jquery extends CI_Model { 

public function test(){

return 

'<script>
$(document).ready(function() {
$("#submitForm").click(function(){
alert("test");
});

});

</script>';
}
}

In the controller I passed the script as following:

$this->load->model('jquery');
$data['testjs'] = $this->jquery->test();
$this->load->view('login/content',$data);

In the content view I echo the script:

...echo $testjs...

Unfortunately the script does not work as expected.

7
  • "Unfortunately the script does not work as expected". How does it work? Commented Apr 18, 2015 at 9:47
  • give us any error you are getting on the console? Commented Apr 18, 2015 at 9:51
  • 2
    You should really stop doing what you're trying to do. Javascript belongs to the view in my opinion, the mode l has completely different responsabilities Commented Apr 18, 2015 at 9:53
  • Hi Patrick, I also tried to put the js code in the view: <script> $(document).ready(function() { $("#submitForm").click(function(){ alert("test"); }); }); </script> But I get this error in the console: Uncaught ReferenceError: $ is not defined Commented Apr 18, 2015 at 9:58
  • I am getting this error: ReferenceError: $ is not defined Commented Apr 18, 2015 at 10:06

2 Answers 2

1

you need to include jquery library. Add this in your header view

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>

and then this code in somewhere in view or header view

jQuery(document).ready(function() {
  jQuery("#submitForm").click(function(){
    alert("this is working file ");
  });
});
Sign up to request clarification or add additional context in comments.

Comments

0

you can use Dom crawler for that if you want to send html and jquery data pass through controller

https://github.com/dimabdc/PHP-Fast-Simple-HTML-DOM-Parser

or else you can set in header view file as above answer given

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.