I am trying to dynamically update a variable in a link depending on the page class, but I cannot seem to get the variable ebox to update in the if/else statement below.

At present the variable is always 0, which is how it is initialised. I have implemented the code here and set up page classes for the home, about and contact pages as per the code below.

However the ebox value never becomes the intended 1, 2, or 3.

Any suggestions as to what I am missing?

$menu  = JFactory::getApplication()->getMenu()->getActive();
$class = $menu->params->get('pageclass_sfx');

    var pageclass = "'<?php echo $class ;?>'";
    var ebox = 0;
    document.write(pageclass, ebox);

    if (pageclass == "home") {
        ebox = 1;
    else if (pageclass == "about") {
        ebox = 2;
    else if (pageclass == "contact") {
        ebox = 3;

    document.write('<a class="btn" data-ebox="' + ebox + '" href="#">Menu</a>');

1 Answer 1


It seems your pageclass value is being declared as 'home' (single-quoted), but you intend for it to be home. Just remove the single quotes, like this:

var pageclass = "<?php echo $class; ?>";

This syntax provides the same effect with improved brevity:

var pageclass = "<?=$class?>";

See this post regarding PHP short tags.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.