1

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?

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

<script>
    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>');
    document.write(pageclass,ebox);
</script>
2

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 acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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