jQuery Selector Question

Discussion in 'Web Design and Development' started by turbobass, Aug 7, 2011.

  1. turbobass macrumors 6502

    Joined:
    May 25, 2010
    Location:
    Los Angeles
    #1
    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>
     
  2. designguy79 macrumors 6502

    Joined:
    Sep 24, 2009
    Location:
    Michigan
    #2
    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>
     
  3. turbobass, Aug 7, 2011
    Last edited: Aug 7, 2011

    turbobass thread starter macrumors 6502

    Joined:
    May 25, 2010
    Location:
    Los Angeles
    #3
    Nothing!

    Yep! Just like that actually, encompassing everything right inside the CDATA tags ;)

    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.

    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.

    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)
    .
     
  4. designguy79 macrumors 6502

    Joined:
    Sep 24, 2009
    Location:
    Michigan
    #4
    Have you looked at jQuery "each"?

    Code:
    $('.commentcount').each(function(index) {
        alert(index + ': ' + $(this).html());
    });
     
  5. turbobass thread starter macrumors 6502

    Joined:
    May 25, 2010
    Location:
    Los Angeles
    #5
    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!!
     
  6. designguy79 macrumors 6502

    Joined:
    Sep 24, 2009
    Location:
    Michigan
    #6
    No worries; believe me, I have been there before, too! Glad to help.
     

Share This Page