views:

45

answers:

3

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/

A: 

You are creating a jQuery object from the element and try to get the id from that. It would would if you use the attr method, but you should just get the id from the element itself:

var planet = this.id;

As for the backup method, that sounds plausible. However, instead of linking to a different page I would link to the same page with a querystring that determines what to show.

Guffa
A: 

$(this).id is where the issue is... use $(this).attr('id')

Teja Kantamneni
+2  A: 

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.

cletus
Well everyone's edits work for getting the right id. Now the problem seems to be that even after I get the right id and perform the addClass or changing the body's background w/ CSS, nothing changes.By the way, the 'return false' at the end. Does that mean that if this function fails (meaning JS is off), that the link will default to the href attribute and follow that?
Mathias Schnell
Could it be because the modified css needs to have the path to the image relative from the html-page, and not the css-file's location? Check the response your getting from the webserver, or your webserver logs, for 404 errors.
nikc
You were right about the pathing, that's been solved. But now my other problem is that the addClass() function doesn't seem to working. In my CSS sheet are classes such as: .earth{color: #2020FF;}, .neptune{color: #4169E1;}, .moon{color: #E4E4E4;}. The point of adding/removing them is simply to change the color of the text in a div box near the center image.
Mathias Schnell
Also, you're right, I am using PHP for my server side scripting. I'm in the process of changing the page such that it simply comes back itself and determines what css to load or create for the desired effect.
Mathias Schnell