0

I am trying to get CakePHP working with JQuery. Below is the div that I have in place, and all I am trying to do is simply alert("CLICKED!"); whenever the below link is clicked.

<?php echo $this->Html->link('Link Click Me', '#', array('onclick'=>'return false;', 'id'=>'divId', 'class'=>'divClass')); ?>

I have the following files 'jquery-1.5.2.js' and 'js_file.js' in the 'js' directory of the 'webroot' folder in my project, and JQuery is working, but I cant seem to get this particular functionality working.

Any suggestions??

Code for jquery:

$(document).ready(function(){

    $('.divClass').click( function(){
        alert("TESTING!");
    });

});

And this is the view:

<?php echo $this->Html->link('Link Click Me', '#', array(  'id'=>'divId', 'class'=>'divClass')); ?>

I am still looking for an answer to this question guys....

5
  • Did you try removing the onclick="return false;" from the link? It might interfere with the jQuery click event. Commented Aug 3, 2011 at 13:10
  • Hi, yes ive removed the code mentioned above, but there is no difference. I cant seem to target any elements (div,span,links etc) from within cakephp. Do you have any other ideas?? Commented Aug 3, 2011 at 13:56
  • You say "jQuery is working" -- how can you tell if you can't target any elements? Commented Aug 3, 2011 at 13:59
  • Also look at the generated html and make sure that the Html helper has actually added the ids and classes to the elements. Commented Aug 3, 2011 at 14:00
  • Because on document.ready() i have an alert() which pops up each time I refresh the page. Targetting elements, however, doesnt seem to be working. Commented Aug 3, 2011 at 14:00

2 Answers 2

2

Post your jQuery code for the alert.

You may need to use bind() instead of click().

Remove onclick="return false;" from the link and add return false; to the end of the jQuery click function.

Sign up to request clarification or add additional context in comments.

2 Comments

I might suggest using preventDefault() rather than return false (fuelyourcoding.com/jquery-events-stop-misusing-return-false), but the placement in the event handler is definitely the way to go.
I hadn't heard of that! Nice one :)
0

The problem seems to be the fact that I didnt include these at the top of the view:

<!-- IMPORTS: -->
<?php $this->Html->css("css_file", null, array("inline"=>false)); ?>
<?php echo $html->script("jquery-1.5.2"); ?>
<?php echo $html->script("js_file"); ?>

1 Comment

Use $this->Html->script(), don't mix and match. Also, did you see the JS helper in CakePHP? book.cakephp.org/view/1592/Js

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.