Jump to content
iwato

Correct Grammar, Bad Message

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

Share this post


Link to post
Share on other sites

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)

  • Thanks 1

Share this post


Link to post
Share on other sites

You could also use $(this) to refer to each element, instead of getting the ID of $(this) and then looking up the same element.

  • Like 1

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
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

×