# Cant get js to work in cakephp3

**URL:** <https://discourse.cakephp.org/t/cant-get-js-to-work-in-cakephp3/1789>\
**Category:** Uncategorized\
**Created:** [December 30, 2016, 8:14am UTC](https://discourse.cakephp.org/t/cant-get-js-to-work-in-cakephp3/1789 "2016-12-30T08:14:42Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![jagguy](https://avatars.discourse-cdn.com/v4/letter/j/b487fb/32.png) [@jagguy](https://discourse.cakephp.org/u/jagguy)\
**Post date:** [December 30, 2016, 8:14am UTC](https://discourse.cakephp.org/t/cant-get-js-to-work-in-cakephp3/1789/1 "2016-12-30T08:14:42Z")

</div>

Hi,

I cant get javascript code to work in cakephp3. Originally I want to hide/show a div section from clicking an option. The project works fine apart from this js function. I have the js function in a file (webroot/js/myjs.js) and I call the file in the layout as below. I click an option button to invoke this function to test it and I get no output. I cant see what else I need to do as I am stuck.

Here is the example  
[crm5.aptutoring.com.au/lessons/cancelledlessontest](http://crm5.aptutoring.com.au/lessons/cancelledlessontest)

```
<?php 

     $options=array(0=>'Student',1=>'Tutor');
echo $this->Form->input('cancelledBy',
       [ 'label'=>false, 'style'=>"margin:10px;", 'type' => 'radio', 'options' => $options,
                    'onclick'=>'hide()', 'value'=>0]);

            ?>

  <div id="cancel" style="display: none;">Hello hidden content</div>

  //myjs.js
   <script type="text/javascript">
  function hide()
   {
     alert('asd');

   var e = document.getElementById("cancel");

    if(e.style.display == 'block')
      e.style.display = 'none';
   else
      e.style.display = 'block';

}
  </script>[crm5.aptutoring.com.au/lessons/cancelledlessontest](http://crm5.aptutoring.com.au/lessons/cancelledlessontest )
```

---

<div class="post-metadata">

**Author:** ![aavrug](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/aavrug/32/178_2.png) [@aavrug](https://discourse.cakephp.org/u/aavrug)\
**Post date:** [December 30, 2016, 8:32am UTC](https://discourse.cakephp.org/t/cant-get-js-to-work-in-cakephp3/1789/2 "2016-12-30T08:32:47Z")

</div>

Why you added `<script type="text/javascript">` in `myjs.js`? Just have a look in console.

---

<div class="post-metadata">

**Author:** ![jagguy](https://avatars.discourse-cdn.com/v4/letter/j/b487fb/32.png) [@jagguy](https://discourse.cakephp.org/u/jagguy)\
**Post date:** [December 30, 2016, 10:04am UTC](https://discourse.cakephp.org/t/cant-get-js-to-work-in-cakephp3/1789/3 "2016-12-30T10:04:15Z")

</div>

[crm5.aptutoring.com.au/lessons/cancelledlessontest](http://crm5.aptutoring.com.au/lessons/cancelledlessontest)

it makes no difference that I have the tags. I removed them and look, no difference.

---

<div class="post-metadata">

**Author:** ![aavrug](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/aavrug/32/178_2.png) [@aavrug](https://discourse.cakephp.org/u/aavrug)\
**Post date:** [December 30, 2016, 10:11am UTC](https://discourse.cakephp.org/t/cant-get-js-to-work-in-cakephp3/1789/4 "2016-12-30T10:11:49Z")

</div>

Add a class to cancelledBy input field and then add this code in your myjs.js

```auto
$('.cancelledByClass').change(function(){
  alert('Here');
  //your code.
});

```

---

<div class="post-metadata">

**Author:** ![jagguy](https://avatars.discourse-cdn.com/v4/letter/j/b487fb/32.png) [@jagguy](https://discourse.cakephp.org/u/jagguy)\
**Post date:** [December 30, 2016, 10:25am UTC](https://discourse.cakephp.org/t/cant-get-js-to-work-in-cakephp3/1789/5 "2016-12-30T10:25:33Z")

</div>

nothing again. Something isnt set up correctly

---

<div class="post-metadata">

**Author:** ![aavrug](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/aavrug/32/178_2.png) [@aavrug](https://discourse.cakephp.org/u/aavrug)\
**Post date:** [December 30, 2016, 10:27am UTC](https://discourse.cakephp.org/t/cant-get-js-to-work-in-cakephp3/1789/6 "2016-12-30T10:27:53Z")

</div>

Please show the code that you tried?

---

<div class="post-metadata">

**Author:** ![jagguy](https://avatars.discourse-cdn.com/v4/letter/j/b487fb/32.png) [@jagguy](https://discourse.cakephp.org/u/jagguy)\
**Post date:** [December 30, 2016, 10:31am UTC](https://discourse.cakephp.org/t/cant-get-js-to-work-in-cakephp3/1789/7 "2016-12-30T10:31:17Z")

</div>

same as what you had

$(’.cancelledByClass’).change(function(){  
alert(‘Here’);  
//your code.  
});

---

<div class="post-metadata">

**Author:** ![jagguy](https://avatars.discourse-cdn.com/v4/letter/j/b487fb/32.png) [@jagguy](https://discourse.cakephp.org/u/jagguy)\
**Post date:** [December 30, 2016, 10:37am UTC](https://discourse.cakephp.org/t/cant-get-js-to-work-in-cakephp3/1789/8 "2016-12-30T10:37:38Z")

</div>

```
        echo $this->Form->input('cancelledBy',
              [ 'class'=>'cancelledByClass','label'=>false, 'style'=>"margin:10px;", 'type' => 'radio', 'options' => $options,
                         'value'=>0]);
```

---

<div class="post-metadata">

**Author:** ![aavrug](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/aavrug/32/178_2.png) [@aavrug](https://discourse.cakephp.org/u/aavrug)\
**Post date:** [December 30, 2016, 10:59am UTC](https://discourse.cakephp.org/t/cant-get-js-to-work-in-cakephp3/1789/9 "2016-12-30T10:59:25Z")

</div>

Basically you syntax not adding the class so try the following code. Tested it’s working

```auto
$('input[name="cancelledBy"]').change(function(){
  alert('Here');
  //your code.
});

```

---

<div class="post-metadata">

**Author:** ![jagguy](https://avatars.discourse-cdn.com/v4/letter/j/b487fb/32.png) [@jagguy](https://discourse.cakephp.org/u/jagguy)\
**Post date:** [December 30, 2016, 11:07am UTC](https://discourse.cakephp.org/t/cant-get-js-to-work-in-cakephp3/1789/10 "2016-12-30T11:07:33Z")

</div>

ok that worked so well done (that wasnt easy!)

$(‘input[name=“cancelledBy”]’).change(function(){  
alert(‘Here’);  
var e = document.getElementById(“cancel”);

```
  if(e.style.display == 'block')
      e.style.display = 'none';
   else
      e.style.display = 'block';

```

});
