-2

To achieve my goal as of this question How to apply a url path in after pseudo element? I want to assign a link to the menu item id.

Within php code I wrote this:

<?php
$app = JFactory::getApplication();
$menu = $app->getMenu();
$item = $menu->getItem(474);
?>

and the jquery code is as you see is :

var $link = $('<a>',{
    class: 'all-news-link',
    href: <?php $item; ?> // **here how to assign the link which is assigned in php?**
});

$('#custom-module .moduletable:nth-child(2) h3').append($link);

update

As per the provided link I used json_encode but also not solved my problem. Its even not alerting now.

<?php
$app = JFactory::getApplication();
$menu = $app->getMenu();
$link1 = $menu->getItem(474);
?>

<script>
$(document).ready(function(){  
var $link = $('<a>',{
    class: 'all-news-link',
    href: <?php echo json_encode( $link1 ); ?>
});
$('#custom-module .moduletable:nth-child(2) h3').append($link);
alert("hi"); // it is not alerting
});

</script>

when href: 'http://www.google.com/' used its alerting "hi" but when I use php json_encode() its even not alerting means jquery is invalid

Community
  • 1
  • 1
Navin Rauniyar
  • 10,127
  • 14
  • 45
  • 68

1 Answers1

0

The link should be a string.

$item = $menu->getItem(474);

returns a stdClass.

You should get the route:

$id = 474;
$link = JRoute::_($menu->getItem($id)->link);

Try this:

$(function(){  
    var $link = $('<a>',{
        class: 'all-news-link',
        href: <?php echo json_encode( $link ); ?>
    });
    $('#custom-module .moduletable:nth-child(2) h3').append($link);
});

This sets href to be a valid Javascript string.

MasterAM
  • 16,283
  • 6
  • 45
  • 66