Oh no! Where's the JavaScript?
Your Web browser does not have JavaScript enabled or does not support JavaScript. Please enable JavaScript on your Web browser to properly view this Web site, or upgrade to a Web browser that does support JavaScript.
Not a member yet? Click here to register.
Forgot Password?

How to add class to current menu item?

Asked Modified Viewed 3,335 times
M
MojzisekJan
M
  • Newbie, joined since
  • Contributed 5 posts on the community forums.
  • Started 4 threads in the forums
  • Started this discussions
asked
Newbie

Hi, how can I add class to current menu item:
echo "<ul>".showsublinks("", "nav-item")."</ul>";
0 replies

3 posts

K
KasteR
K
KasteR 10
  • Senior Member, joined since
  • Contributed 290 posts on the community forums.
  • Started 1 thread in the forums
answered
Senior Member

Alternatively you may also want to consider the jQuery addClass function.

CSS:
[syntaxhighlighter brush=css,first-line=1,highlight=0,collapse=false,html-script=false] <style>
.newClass{
color: #ff0000;
}
</style>[/syntaxhighlighter]
HTML:
[syntaxhighlighter brush=html,first-line=1,highlight=0,collapse=false,html-script=false] <div id="nav">Testing content here</div><br />
<button onclick="addClass()">Click me</button>[/syntaxhighlighter]
Javascript:
[syntaxhighlighter brush=javascript,first-line=1,highlight=0,collapse=false,html-script=false]<script type="text/javascript">
function addClass()
{
$("#nav"wink.addClass("newClass"wink;
}
</script>[/syntaxhighlighter]
Complete Example Script:
[syntaxhighlighter brush=php,first-line=1,highlight=0,collapse=false,html-script=false]<?php
// Testing JQUERY add class function
require_once "maincore.php";
require_once THEMES."templates/header.php";
include LOCALE.LOCALESET."downloads.php";
add_to_head('
<style>
.newClass{
color: #ff0000;
}
</style>
'wink;

opentable('Testing CSS Function'wink;
echo '
<div id="nav">Testing content here</div><br />
<button onclick="addClass()">Click me</button>
';
closetable();

add_to_footer('<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>'wink;
add_to_footer('<script type="text/javascript">
function addClass()
{
$("#nav"wink.addClass("newClass"wink;
}
</script>'wink;
require_once THEMES."templates/footer.php";
?>[/syntaxhighlighter]
Edited by KasteR on 29-08-2013 01:38,
0 replies
C
Chan
C
Chan 0
Lead Developer of PHP-Fusion
  • Super Admin, joined since
  • Contributed 3,842 posts on the community forums.
  • Started 232 threads in the forums
  • Answered 6 questions
answered
Super Admin

Now this is something I can never do. Lol great stuff!
0 replies
F
Falk
F
Falk 146
Need help?, Having trouble?
• View our Documentation for Guides, Standards and Functions
• Name and Organize your Topics and Content correctly in the corresponding Forums for best support results
• Attaching Log Files and Screenshots when reporting issues will help
• Provide with an URL to live example if one exists
• Please read the How to Report an Error post
• Please read and comply with the Code of Conduct

(¯·._.·(¯°·._.·°º*[ Project Manager ]*º°·._.·°¯)·._.·¯)
  • Super Admin, joined since
  • Contributed 6,201 posts on the community forums.
  • Started 639 threads in the forums
  • Answered 12 questions
answered
Super Admin

here is a dirty hack I use here and there.

echo "<script type='text/javascript'>
$(document).ready(function() {
 $(function(){
   var path = location.pathname.substring(1);
   if ( path )
     $('#nav a[href$=\"' + path + '\"]').attr('class', 'current');
  });
});
</script>";


#nav is the ID of your class item, current is the class that will be added if the URL matches ID.
0 replies

Category Forum

Themes Support

Labels

None yet

Statistics

  • Views 0 views
  • Posts 3 posts
  • Votes 0 votes
  • Topic users 4 members

4 participants

F
F
Falk 146
Need help?, Having trouble?
• View our Documentation for Guides, Standards and Functions
• Name and Organize your Topics and Content correctly in the corresponding Forums for best support results
• Attaching Log Files and Screenshots when reporting issues will help
• Provide with an URL to live example if one exists
• Please read the How to Report an Error post
• Please read and comply with the Code of Conduct

(¯·._.·(¯°·._.·°º*[ Project Manager ]*º°·._.·°¯)·._.·¯)
  • Super Admin, joined since
  • Contributed 6,201 posts on the community forums.
  • Started 639 threads in the forums
  • Answered 12 questions
C
C
Chan 0
Lead Developer of PHP-Fusion
  • Super Admin, joined since
  • Contributed 3,842 posts on the community forums.
  • Started 232 threads in the forums
  • Answered 6 questions
K
K
KasteR 10
  • Senior Member, joined since
  • Contributed 290 posts on the community forums.
  • Started 1 thread in the forums
M
M
  • Newbie, joined since
  • Contributed 5 posts on the community forums.
  • Started 4 threads in the forums
  • Started this discussions

Notifications

Track thread

You are not receiving notifications from this thread.

Related Questions

Not yet