Need some quick jQuery help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Lace
    Too lazy to set a custom title
    • Mar 2004
    • 16116

    #1

    Need some quick jQuery help

    Trying to code up a page to hide content on load, but expand once the header is clicked.

    First header works, but the second and third are not at the moment. I need probably 15 different headers here. I'm no jQuery expert by far.

    http://barcritic.com/toggle3.html

    Anyone see what I'm doing wrong here?

    Code:
    $(document).ready(function(){
    	$("#expanderHead").click(function(){
    		$("#expanderContent").slideToggle();
    		if ($("#expanderSign").text() == "+"){
    			$("#expanderSign").html("−")
    		}
    		else {
    			$("#expanderSign").text("+")
    		}
    	});
    });
    Code:
    <h4 id="expanderHead">
    	HEADER 1 <span id="expanderSign">+</span>
    </h4>
    <div id="expanderContent" style="display:none">
    TEXT HERE
    </div>
    
    
    <h4 id="expanderHead">
    	HEADER 2 <span id="expanderSign">+</span>
    </h4>
    <div id="expanderContent" style="display:none">
    TEXT HERE
    </div>
    
    
    <h4 id="expanderHead">
    	HEADER 3 <span id="expanderSign">+</span>
    </h4>
    <div id="expanderContent" style="display:none">
    TEXT HERE
    </div>
    Last edited by Lace; 08-15-2012, 03:07 PM.
    Your Paysite Partner
    Strength In Numbers!
    StickyDollars | RadicalCash | KennysPennies | HomegrownCash
  • sarettah
    see you later, I'm gone
    • Oct 2002
    • 14297

    #2
    I am not a jquery expert but I think your problem is because of duplicate ids. I do believe that ids need to be unique.

    Just a quick guess.

    .
    All cookies cleared!

    Comment

    • Lace
      Too lazy to set a custom title
      • Mar 2004
      • 16116

      #3
      Originally posted by sarettah
      I am not a jquery expert but I think your problem is because of duplicate ids. I do believe that ids need to be unique.

      Just a quick guess.

      .
      Writing duplicate script and css with different ID's would be quite tedious, extensive and overkill though. I have another example without the - / +, but I need that.


      Here's the other one.
      Code:
      jQuery(document).ready(function() {
        jQuery(".content").hide();
        //toggle the componenet with class msg_body
        jQuery(".heading").click(function()
        {
          jQuery(this).next(".content").slideToggle(500);
        });
      });
      Last edited by Lace; 08-15-2012, 03:20 PM.
      Your Paysite Partner
      Strength In Numbers!
      StickyDollars | RadicalCash | KennysPennies | HomegrownCash

      Comment

      • sarettah
        see you later, I'm gone
        • Oct 2002
        • 14297

        #4
        Originally posted by Lace
        Writing duplicate script and css with different ID's would be quite tedious, extensive and overkill though. I have another example without the - / +, but I need that.
        I have dome similar coding with just straight javascript and passing an id into a function.

        I have not used jquery much though so I am not sure of the best way to do it there.

        Doing it with just javascript on the onclick is fairly easy.


        .
        All cookies cleared!

        Comment

        • Lace
          Too lazy to set a custom title
          • Mar 2004
          • 16116

          #5
          Hmm...going to try this http://jsfiddle.net/sYwER/5/

          Just might work for what I need.

          Edit: Nope, unordered and ordered lists are not hidden.
          Last edited by Lace; 08-15-2012, 03:32 PM.
          Your Paysite Partner
          Strength In Numbers!
          StickyDollars | RadicalCash | KennysPennies | HomegrownCash

          Comment

          • sarettah
            see you later, I'm gone
            • Oct 2002
            • 14297

            #6
            Originally posted by Lace
            Hmm...going to try this http://jsfiddle.net/sYwER/5/

            Just might work for what I need.

            Edit: Nope, unordered and ordered lists are not hidden.
            Taking what you had and then a little from what you found:

            Script:

            Code:
            $(document).ready(function(){
            	$(".head").click(function(){
            		$(this).find(".expanderContent").slideToggle();
            		if ($(this).find(".expanderSign").text() == "+"){
            			$(this).find(".expanderSign").html("−")
            		}
            		else {
            			$(this).find(".expanderSign").text("+")
            		}
            	});
            });
            Changed up the html a touch to wrap each of your sections in a div:

            Code:
            <div class=head>
            <h4 class="expanderHead">
            	HEADER 1<span class="expanderSign">+</span>
            </h4>
            <div class="expanderContent" style="display:none">
            TEXT HERE
            </div>
            </div>
            
            <div class=head>
            <h4 class="expanderHead">
            	HEADER 2 <span class="expanderSign">+</span>
            </h4>
            <div class="expanderContent" style="display:none">
            TEXT HERE
            </div>
            </div>
            
             <div class=head>
            <h4 class="expanderHead">
            	HEADER 3 <span class="expanderSign">+</span>
            </h4>
            <div class="expanderContent" style="display:none">
            TEXT HERE
            </div>
            </div>
            http://madspiders.com/divtest.htm
            Last edited by sarettah; 08-15-2012, 04:18 PM.
            All cookies cleared!

            Comment

            • Lace
              Too lazy to set a custom title
              • Mar 2004
              • 16116

              #7
              Awesome!!! Thanks, sarettah!
              Your Paysite Partner
              Strength In Numbers!
              StickyDollars | RadicalCash | KennysPennies | HomegrownCash

              Comment

              Working...