Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

turbobass

macrumors 6502
Original poster
May 25, 2010
294
3
Los Angeles
Spent 2 days trying to figure this out: in the code below the true condition for the "if" will cause ALL <div class="commentcount"> to read "castor" ... if the condition is false it will read "pollux" on ALL <div class="commentcount">.

I have solved this issue before by using the $(this) provided by jQuery unfortunately this time I can't figure out how to get it in there without breaking the if / else from actually making the replacement.

Please note that I have constructed this example to be simple, this is not production code...Does ANYONE know how to get this working?

Code:
<script>
//<![CDATA[

var postID = <?php the_id() ?>;	

if (postID > 0) {
	$(".commentcount").html('castor');
}

else {
	$(".commentcount").html('pollux');
}

//]]>
</script>
 
It looks correct to me... here are the places I would start:

1. What does Firefox "Error Console" report?

2. Are you using jQuery's "document ready" function properly? e.g...

Code:
$(document).ready(function() {
  // run code here when the document is fully loaded
});

3. Do you have other JavaScript libraries that might be using the "$" shortcut? If so, check out this page.

4. Make sure your PHP function "the_id()" is outputting a number

5. Do you have to use jQuery? Maybe you could use a PHP "if" statement around the relevant HTML block(s) directly?

Code:
<div class="commentcount">
<?php

if(the_id() > 0)
  echo 'castor';
else
  echo 'pollux';

?>
</div>
 
It looks correct to me... here are the places I would start:

1. What does Firefox "Error Console" report?
Nothing!

2. Are you using jQuery's "document ready" function properly? e.g...

Code:
$(document).ready(function() {
  // run code here when the document is fully loaded
});
Yep! Just like that actually, encompassing everything right inside the CDATA tags ;)

3. Do you have other JavaScript libraries that might be using the "$" shortcut? If so, check out this page.
Maybe the Facebook or Google library? Checking this out now, however I have other jQuery based functions using the "$" shortcut now with no problem selecting based on child elements.

4. Make sure your PHP function "the_id()" is outputting a number
It is. Variable seems to be working when I change the integer operand to test, but hard to tell since it looks like the
Code:
else
will always override the
Code:
if
as long as it fires on an "else" that comes after.

5. Do you have to use jQuery? Maybe you could use a PHP "if" statement around the relevant HTML block(s) directly?

Code:
<div class="commentcount">
<?php

if(the_id() > 0)
  echo 'castor';
else
  echo 'pollux';

?>
</div>
Good thought, I've considered it -- but the reason I am using jQuery is because this little snippet is actually part of a larger JavaScript call I am making to Facebook's Graph API. I am just working on this little tidbit now to get the class selection down because when I'm done with this the variable containing that "the_id()" function is going to actually be assigned a JavaScript comment count.

All of this DOES perform the replacement it's just that it's appearing multiple times on the same page in each instance of the post in the blog ... so the server is giving each one a different the_id() value, and my hope is that the
Code:
if/else
dealing with different the_id() values will overwrite JUST the instance of the class specified ... guess I'll keep playing with
Code:
$(this)
.
 
Last edited:
Have you looked at jQuery "each"?

Code:
$('.commentcount').each(function(index) {
    alert(index + ': ' + $(this).html());
});
I'm laughing my ass off over here, I don't know HOW many times I've tried this but just because you said it I was like "Darned designguy79 telling me to do this, I already tried it but I don't want to seem like a total dummy so I'll give it a shot again..."

Guess I'm a total dummy. :D:D:D

Thank you very, very much!!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.