3

I am trying add simple bootstrap jquery functionality in cakephp 2.5.5but i dont know how to add script in view.Please suggest me some code?

in webroot jquery folder i have created custom.js

$(document).ready(function(){ $("#hide").click(function(){ $("p").hide(); }); }); 

in view i have

<?php
echo $this->Html->css('index'); 
echo $this->Html->script('custom');
?>
 <p ><?php echo "some text"; ?></p>
<?php echo $this->Form->button('click me',array('id'=>'hide'));?>
3
  • what did you try so far? Commented Nov 26, 2014 at 6:11
  • Check CakePHP documentation book.cakephp.org/2.0/en/core-libraries/helpers/js.html Commented Nov 26, 2014 at 6:31
  • In Stackoverflow any question is editable, so any required details, you are able to add them to your question not in comment, specially, those have code! Commented Nov 26, 2014 at 6:33

1 Answer 1

1

Say path to custom.js file is in webroot/js/jquery/custom.js, then you can have it in view with

<?php echo $this->Html->script('jquery/custom.js'); ?>

If the path is not in the folder webroot/js (say webroot/jquery/custom.js), then specify the whole path to the file like

<?php echo $this->Html->script('/jquery/custom.js'); ?>

Always add the line below at at the end as described here

echo $this->Js->writeBuffer();
Sign up to request clarification or add additional context in comments.

7 Comments

What is it that you actually failed to do? The script execution or adding the script to the page. To see if the script is on page view the source and see if you can see that portion of code in the souce. Otherwise, look at the selector, give an id to p as follows: <p id="givenID">Some TExt</p> In your script in place of ( $("p")) write ( $("#givenID"))
When i click that button ,it wil not hide paragraph
Have you downloaded and imported jquery library? If not that's the first thing you need to do before.
I have added bootstrap jquery library
do you have echo $this->Js->writeBuffer(); as described here book.cakephp.org/2.0/en/core-libraries/helpers/js.html
|

Your Answer

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