jagguy
December 30, 2016, 8:14am
1
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
<?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 )
aavrug
December 30, 2016, 8:32am
2
Why you added <script type="text/javascript">
in myjs.js
? Just have a look in console.
jagguy
December 30, 2016, 10:04am
3
crm5.aptutoring.com.au/lessons/cancelledlessontest
it makes no difference that I have the tags. I removed them and look, no difference.
aavrug
December 30, 2016, 10:11am
4
Add a class to cancelledBy input field and then add this code in your myjs.js
$('.cancelledByClass').change(function(){
alert('Here');
//your code.
});
jagguy
December 30, 2016, 10:25am
5
nothing again. Something isnt set up correctly
aavrug
December 30, 2016, 10:27am
6
Please show the code that you tried?
jagguy
December 30, 2016, 10:31am
7
same as what you had
$(’.cancelledByClass’).change(function(){
alert(‘Here’);
//your code.
});
jagguy
December 30, 2016, 10:37am
8
echo $this->Form->input('cancelledBy',
[ 'class'=>'cancelledByClass','label'=>false, 'style'=>"margin:10px;", 'type' => 'radio', 'options' => $options,
'value'=>0]);
aavrug
December 30, 2016, 10:59am
9
Basically you syntax not adding the class so try the following code. Tested it’s working
$('input[name="cancelledBy"]').change(function(){
alert('Here');
//your code.
});
jagguy
December 30, 2016, 11:07am
10
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';
});