Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 583389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:47:15+00:00 2026-05-13T14:47:15+00:00

This code is supposed to get the id from a clicked anchor tag in

  • 0

This code is supposed to get the id from a clicked anchor tag in the middle of a ul with the id ‘links’ and then use that id for other purposes across the page, however the alerts have shown me that I keep getting ‘undefined’ as if the element has no id. I double check my source code and they do indeed have ids, so I’m not sure what the problem is.

$(function() {      
    $('ul#links > li > a').click(function() {

        var planet = $(this).id;
        alert(planet);
        $('#planet').removeClass();
        $('#planet').addClass('planet');

        var bg = 'black url(../img/' + planet + '.jpg) fixed no-repeat center';
        alert(bg);
        $('body').css('background', bg);
    });             
});

Also this Jquery function is being used as a Javascript way of handling this kind of situation. I already had implemented a PHP solution which involved being redirected to a quick script that sets a cookie and returns the user to the previous page, then using the cookie to load a new CSS. Is there a way I could use that method as a backup to the JS method in the case that JS is disabled?

The page – http://schnell.dreamhosters.com/folio/

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T14:47:16+00:00Added an answer on May 13, 2026 at 2:47 pm

    You were pretty close:

    $(function() {      
      $('ul#links > li > a').click(function() {
        var planet = this.id;
        $('#planet').removeClass().addClass("plane");
        var bg = 'black url(../img/' + planet + '.jpg) fixed no-repeat center';
        $('body').css('background', bg);
        return false;
      });             
    });
    

    Firstly, this.id will get you the ID. The jQuery alternative is $(this).attr("id").

    Second, you can chain addClass() and removeClass() to save looking up the element twice. This isn’t necessary for your problem but is good practice.

    Third, unless you want your link to traverse somewhere you need to either prevent the default action (by calling evt.preventDefault() on the event object passed to the callback) or simply ending with return false; (which also does evt.stopPropagation()).

    Lastly, what you’re doing is well-suited to working with Javascript disabled because you can go to a page that does the same thing. I wouldn’t bother with setting the cookie and redirecting. If the script name URI is /view/planet.php do this:

    <a id="saturn" href="/view/planet.php?planet=saturn">Saturn</a>
    

    Inside the same script you can do (I’m assuming PHP here; I didn’t see any mention of what you use):

    $planets = array(...);
    $planet = $_GET['planet'];
    if (in_array($planet, $planets)) {
      echo <<<END
    <style type="text/css">
    body { background: black url(.../img/$planet.jpg) fixed no-repeat center; }
    </style>
    END;
    }
    

    while rendering the page.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This code is from Prototype.js . I've looked at probably 20 different tutorials, and
Suppose I have this code: var myArray = new Object(); myArray["firstname"] = "Bob"; myArray["lastname"]
Suppose to have a code like this: <div class=notSelected> <label>Name <input type=text name=name id=name
Suppose I have code like this: template<class T, T initial_t> class Bar { //
I've seen second one in another's code and I suppose this length comparison have
This code in JS gives me a popup saying i think null is a
this code always returns 0 in PHP 5.2.5 for microseconds: <?php $dt = new
This code works (C# 3) double d; if(d == (double)(int)d) ...; Is there a
This code always works, even in different browsers: function fooCheck() { alert(internalFoo()); // We
This code does not seem to compile, I just need to write something to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.