Jump to content

Javascript Active State change


stevejvan

Recommended Posts

Ok, I am very new to javascript so I apologize if this is an easy fix.  I have searched google, and everything I find isn't helpful.

 

I have a portfolio site, that has an active state for each item on the nav bar using the body tag.  Once you get to the portfolio page, there are genre links in addition to the nav bar links.  I want to have a secondary active state for the genres, the genre code is inserted using a PHD includes statement, so there aren't multiple pages, just one main page with sections of html that change through php.

 

I have been trying to find a way to change the body class after clicking on the secondary link.

 

I found this code online...doesn't work.

 

function newclass(){

document.getElementsBytagName('body')[0].id="seniors"}

 

<a href="?page=seniors" class="genreseniors" onclick="newclass1()">

 

I am not sure if the body tag is the best way to go, or if i could do an if>then statement using the page=...

 

I don't know.

 

Any help would be appreciated.  Thank you!

 

Link to comment
https://forums.phpfreaks.com/topic/166834-javascript-active-state-change/
Share on other sites

Seeing this:

I want to have a secondary active state for the genres, the genre code is inserted using a PHD includes statement, so there aren't multiple pages, just one main page with sections of html that change through php.

 

(PHD include  :P gimme one of those please)

In that text it looks like your using the php include function. You can't use the include clientside unless you're using ajax.

 

your hyperlink

<a href="?page=seniors" class="genreseniors" onclick="newclass1()">

Here it shows that your hyperlink action will load the same page again with url parameters there for the onclick doesn't make much sence.

 

I'm assuming you're using a $_GET to see what to include such as:

<?php
if($_GET['page']=='seniors'){
  include ("seniors.php");
}elseif($_GET['page']=='something else'){
  include ("somethingelse.php");
}
?>

 

If the above looks familiar to your code you're better of doing it all in php instead of trying to use javascript to change a css class. Using an onclick event would be pointless in that case since the page simply refreshes and undoing the javascript onclick action.

 

You might want to look up what the difference is between clientside and serverside.

Hope it helps.

You are right, I am using the GET statement you included sorry I said Includes.

 

So could you give me an example of how I could change the position of a <li> image using php?

 

<a href="?page=seniors" class="genreseniors" <?php echo(isset($_GET['page'])&& $_GET['page']=='seniors')?'class="highlight"':"" ;?> >link</a>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.