george Posted November 13, 2013 Share Posted November 13, 2013 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 More sharing options...
dsonesuk Posted November 13, 2013 Share Posted November 13, 2013 How i read this, is that you get id ref of div, then find p element with SAME id ref and show child pre with class green, there's your problem you can't have multiple identical id reference within a page they have to be unique. Link to comment Share on other sites More sharing options...
Hadien Posted November 13, 2013 Share Posted November 13, 2013 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now