Jump to content

Correct Grammar, Bad Message


iwato

Recommended Posts

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

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 by iwato
Link to comment
Share on other sites

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.

  • Like 1
Link to comment
Share on other sites

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

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

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...