In my layout, i am referring to a jquery function when you click on a div
echo '<div class="fright menu_opcion" onclick="cambiarMenu(-1, \'users\', \'panel\');">Inicio</div>';
echo '<div class="fright menu_opcion" onclick="cambiarMenu(-1, \'eventos\', \'index\');">Eventos</div>';
echo '<div class="fright menu_opcion" onclick="cambiarMenu(-1, \'eventos\', \'add\');">Crear evento</div>';
HTML code :
<div class="fright menu_opcion" onclick="cambiarMenu(-1, 'users', 'panel');">Inicio</div>
<div class="fright menu_opcion" onclick="cambiarMenu(-1, 'eventos', 'index');">Eventos</div>
<div class="fright menu_opcion" onclick="cambiarMenu(-1, 'eventos', 'add');">Crear evento</div>
But always get this error:
function :
<script>
cambiarMenu = function( evento_id, controller, action ) {
if (evento_id == 0)
return;
url='<?php echo $this->Html->url(array('admin'=>'true',
'plugin'=>null,
'controller' => 'menus',
'action' => 'menu_change'));?>/'+evento_id;
$.post(url, function(data) {
if(data)
if (data != -1){
window.location.href = '<?php if ($SuperAdmin) $action = 'view'; else $action = 'view';
echo $this->Html->url(array('admin'=>'true',
'plugin'=>null,
'controller' => 'events',
'action' => $action));?>';
} else {
redirect = ('<?php echo $this->Html->url(array('admin'=>'true',
'plugin'=>null,
'controller'=>'',
'action' => ''));?>/').split('admin');
window.location.href = redirect[0] + 'admin/'+controller+'/'+action;
}
}
);
}
This function refers to where to redirect the user, I am using Cakephp why the code is.
Uncaught ReferenceError: cambiarMenu is not defined
-
If it's a jQuery function, shouldn't there be a dollar sign somewhere?!Karl-André Gagnon– Karl-André Gagnon2016年04月20日 18:23:57 +00:00Commented Apr 20, 2016 at 18:23
-
Sorry! add the function to my post! @Karl-AndréGagnonhateful– hateful2016年04月20日 18:26:11 +00:00Commented Apr 20, 2016 at 18:26
-
1Can you pastebin the rendered HTML? Is your javascript included on the final page?Uzbekjon– Uzbekjon2016年04月20日 18:35:51 +00:00Commented Apr 20, 2016 at 18:35
-
I did not understand what you need @Uzbekjonhateful– hateful2016年04月20日 18:40:07 +00:00Commented Apr 20, 2016 at 18:40
-
2Show us the HTML from the browser, not PHP file.Uzbekjon– Uzbekjon2016年04月20日 18:44:10 +00:00Commented Apr 20, 2016 at 18:44
1 Answer 1
Is the javascript function defined in the page? or Is the javascript file containing the function included correctly? It could be a timing issue.
Read further if you want a way to do this that's cleaner, more reliable, and easier to debug...
Do this with a jQuery event listener:
<script>
function cambiarMenu(val1,val2,val3)
{
alert('cambiarMenu called with '+val1+','+val2+','+val3);
}
$(document).ready(function(){
$(".menu_opcion")
.click(function(){
var menuVal = $(this).html();
switch(menuVal)
{
case "Inicio":
cambiarMenu(-1, 'users', 'panel');
break;
case "Eventos":
cambiarMenu(-1, 'eventos', 'index');
break;
case "Crear evento":
cambiarMenu(-1, 'eventos', 'add');
break;
default:
}
});
/*etc etc etc....*/
});
</script>
<div class="fright menu_opcion">Inicio</div>
<div class="fright menu_opcion">Eventos</div>
<div class="fright menu_opcion">Crear evento</div>
Then again I'm biased against using a php echo statement and a zillion quote escapes for every line of html, and putting javascript in html onClick attributes when it can be avoided.
Stuff's a lot easier to debug...
$(document).ready... handles loading issues. You can also chain other events using this handler strategy, such as mouseover etc:
$(".menu_opcion")
.click(function(){
var menuVal = $(this).html();
switch(menuVal)
{
case "Inicio":
cambiarMenu(-1, 'users', 'panel');
break;
case "Eventos":
cambiarMenu(-1, 'eventos', 'index');
break;
case "Crear evento":
cambiarMenu(-1, 'eventos', 'add');
break;
default:
}
})
.mouseover(function(){})
.mouseout(function(){});
/*etc etc etc....*/
Please note that the semicolon only goes at the end of the chain, not after each event... Cheers!