iwato Posted October 9, 2017 Share Posted October 9, 2017 Please consider the two following sets of code and explain why the Javascript not work and suggest an alternative where possible. There are zero complaints from the Javascript console. None. And, the var podType is properly identified. I have checked with console.(log) The procedure does not even turnover for the final element. I am baffled. <script> $(document).ready(function() { $('li.podDex').each(function(){ var podType = $(this).attr('id'); $('#podType').mouseover(function() { $(this).css({"cursor": "pointer", "font-weight":"800"}); }) .mouseout(function() { $(this).css("font-weight", "normal"); }); $('#podType').mouseover(function() { $(this).css({"cursor": "pointer", "font-weight":"800"}); }) .click(function() { var chrono_var = $("#chronology_div").html(); $("#main").html(chrono_var); }) .mouseup(function() { $(this).css({"color": "#fadb9d","font-weight": "normal"}); $('body, html').animate({scrollTop: $('#main').offset().top},800); }); }); }); </script> <ul> <li id='linear' class='podDex'>Linear Analysis</li> <li id='clausal' class='podDex'>Clausal Analysis</li> <li id='inversion' class='podDex'>Socratic Inversion</li> <li id='chronology' class='podDex'>Chronology</li> </ul> Mind you, in isolation, when podType refers directly to the appropriate id attribute, what is included in the .each() function works fine! Roddy Link to comment Share on other sites More sharing options...
justsomeguy Posted October 9, 2017 Share Posted October 9, 2017 You're telling to find an element with the ID "podType". If you want to use the value of the variable, you need to specify that. $('#' + podType) 1 Link to comment Share on other sites More sharing options...
justsomeguy Posted October 9, 2017 Share Posted October 9, 2017 You could also use $(this) to refer to each element, instead of getting the ID of $(this) and then looking up the same element. 1 Link to comment Share on other sites More sharing options...
iwato Posted October 10, 2017 Author Share Posted October 10, 2017 (edited) No. This $('#' + podType). does not work. And, for the same reason, I believe $(this) will not work. There is something deeper that is afoul. Roddy Edit: No, I take it back. When placed inside the $.ready( ) function it works fine. I am not the hopeless case that I originally thought. Edited October 10, 2017 by iwato Link to comment Share on other sites More sharing options...
justsomeguy Posted October 10, 2017 Share Posted October 10, 2017 If $(this).attr('id') returns the correct ID, then this (and the jQuery-wrapped $(this)) refer to the same element. There's no reason to get the ID from the element, then get the element again. You already have it, you're getting the ID from it. 1 Link to comment Share on other sites More sharing options...
iwato Posted October 10, 2017 Author Share Posted October 10, 2017 I understand, but I need the variable for another process. The anonymous function associated with .click() method is just a placeholder. Link to comment Share on other sites More sharing options...
justsomeguy Posted October 10, 2017 Share Posted October 10, 2017 What variable, podType? That's fine, you can do whatever you want with it (although it only exists inside that function you wrote), I'm just saying there's no need to try to get the same element again when you already have it. 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