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
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.

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.