Jump to content

jQuery question


george

Recommended Posts

I have some jQuery code thar works just fine:

$("a div").hover(function(){var token = $(this).attr('id');alert(token);} 

my alert(token) gives me just what I want it to.

My problem is that I want to combine the variable 'token' with a litteral selector, where the selector is a P element with an ID of token. Then to further complicate my lfe, I want to access not the selector with the ID of token, but it's direct child, which is a <pre element with a class of green. So, I tried :

$("p#"+token+" > pre.green").show();

since that is what I want to show. But nothing shows. Is my selector valid? And if not, how can I make it valid.

 

TIA <- someday I will have a job, and will be able to donate to w3school's forum.

Link to comment
Share on other sites

if you're still using this inside the hover event you don't even have to access the id (even though having 2 different elements use the same id is bad enough and that's the main reason it's not working), you're pretty much breaking the wheel so that you can reinvent it

$("a div").hover(    function(){        $("pre.green",this).show();    },    function(){        $("pre.green",this).hide();    });

in this example i'm using the $(selector,context) format and "this" is referring to the element that is firing the hover event. so it will only look for the pre tags inside that hovered div which have the green class. you might need to use stopPropagation(so that it won't bubble up and out to snag other pre.green tags ), but i can't recall for hover.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...