views:

42

answers:

3

I don't know what's going on here..I'm trying to add a slidetoggle on my menu .. seems very simple .. I've tried to simplify this to try and find the problem (meaning I've taken all the links out and extra jquery, to only have that bottom example - the paragraph at the end) but I don't know what the error is.. (other that it doesn't do anything)

Includes:

<script type="text/javascript" src="/raceday/Scripts/jquery-1.4.2.min.js"></script> 

The script:

<script type="text/javascript">
$("button").click(function() {
    $("p").slideToggle("slow");
});    

The HTML:

<div class="ttl-area">
   <h2 class="ttl-account"><span>Account</span></h2>
</div>
<div class="account-area">
   <div class="login-holder">

      <p><strong>Welcome, <%= ViewModel.Profile.Name.First %></strong></p>

      <ul class="account-links">
         <span id="loginTitle">User Options</span><br /><br />
         <li>
            <%= Html.ActionLink<EventController>( x => x.List(), "All Events" )%>
         </li>
         <li>
            <%= Html.ActionLink<MyEventsController>( x => x.List(), "My Events" )%>
         </li>
         <li>
            <%= Html.ActionLink<AccountController>( x => x.Edit(), "My Profile" )%>
         </li>
         <li>
            <%= Html.ActionLink<ClubController>( x => x.List(), "All Clubs" )%>
         </li>
         <li>
            <%= Html.ActionLink<MyClubsController>( x => x.List(), "My Clubs" )%>
         </li>
         <li>
            <%= Html.ActionLink<AccountController>( x => x.ChangePassword(), "Change My Password" )%>
         </li>
         <li>
            <%= Html.ActionLink<DependantController>( x => x.List(), "My Dependants" ) %>
         </li>

      </ul>
   </div>
  <% if ( ViewModel.Profile.HasOrganizerInfo ) { %>
  <div class="login-holder">
     <ul class="account-links">
        <span id="loginTitle">Organizer Details</span><br /><br />
        <li>
           <%= Html.ActionLink<AccountController>( x => x.Organizer(), "Organizer Details" )%>
        </li>
        <li>
           <%= Html.ActionLink<EventController>( x => x.Edit( default(int?) ), "Post An Event" )%>
        </li>
        <li>
           <%= Html.ActionLink<EventAdminController>( x => x.List(), "Events Created By Me" ) %>
        </li>
        <li>
           <%= Html.ActionLink<ClubController>( x => x.Edit( default( int? ) ), "Create A Club" )%>
        </li>
        <li>
           <%= Html.ActionLink<ClubAdminController>( x => x.List( ), "Clubs Created By Me" )%>
        </li>
         <!-- if ( ViewModel.Profile.IsAdministrator ) { -->
         <li>
            <%= Html.ActionLink<EventReportController>( x => x.List(), "Event Report" ) %>
         </li>
         <!-- } -->
     </ul>
  </div>
  <% } %>
  <% if ( ViewModel.Profile.HasTimerInfo ) { %>
  <div class="login-holder">
     <ul class="account-links">
        <span id="loginTitle">Timer Details</span><br /><br />
        <li>
           <%= Html.ActionLink<AccountController>( x => x.Timer(), "Timer Details" )%>
        </li>
        <li>
           <%= Html.ActionLink<EventTimerController>( x => x.List(), "Events Timed By Me" ) %>
        </li>
     </ul>
  </div>
  <% } %>
      <ul class="account-links">
      <% if ( ( !ViewModel.Profile.HasOrganizerInfo ) || ( !ViewModel.Profile.HasTimerInfo) ) { %>   
         <span id="loginTitle">Additional Options</span><br /><br />
      <% } %>         
         <% if ( !ViewModel.Profile.HasTimerInfo ) { %>
            <li>
               <%= Html.ActionLink<AccountController>( x => x.Timer(), "I Time Events" )%>
            </li>
         <% } %>

         <% if ( !ViewModel.Profile.HasOrganizerInfo ) { %>
            <li>
               <%= Html.ActionLink<AccountController>( x => x.Organizer(), "I Organize Events" )%>

            </li>
         <% } %>

            <li><%= Html.ActionLink<AccountController>( x => x.Logout(), "Log Out" ) %></li>
      </ul>

</div>
        <button>Hide 'em</button>

  <p>Hiya</p>
  <p>Such interesting text, eh?</p>
+1  A: 

you need to wrap it a load method:

$(document).load(function(){
    $("button").click(function() {
        $("p").slideToggle("slow");
    });
})
RobertPitt
I did have that .. still no dice
Kevin
`$(document).load(func...` doesn't work. You can use `$(document).ready(func...` (or its shortcut, see Nick's answer), or `$(window).load(func...` if you want all elements to be fully loaded.
patrick dw
@patrick - good catch on the `.load()` completely glanced over that.
Nick Craver
A: 

tried something like this?

jQ = jQuery.noconflict();

jQ("button").click(function() {
    jQ("p").slideToggle("slow");
});    

maybe asp is messing with your $

Nico
+5  A: 

Your code needs to be in a document.ready handler so the $("button") selector finds elements to bind click handlers to.

$(function() {
  $("button").click(function() {
    $("p").slideToggle("slow");
  });   
});

If the DOM isn't ready yet, the <button> elements may not be added/ready either, which means that $("button") will still bind to all elements it finds...but it won't find them, resulting in a total lack of behavior, which is what you're seeing.

Nick Craver
got it - thxAny idea on how to save the state?
Kevin
@Kevin - The [jQuery cookie plugin](http://plugins.jquery.com/project/Cookie)!
Nick Craver
very helpful! Dare I go for one more?a) I want to have the script for each header, but I don't want to have 20 different functions to slideToggle
Kevin
b) I want to have a +/- to show hide ..
Kevin
@Kevin - In that case you find the element relatively, say each was beside the `<div class="login-holder">`, then instead of `$("<p>")` you might do `$(this).siblings(".login-holder").slideToggle();`, make sense?
Nick Craver
yeah but that didn't really work well with that <div> .. clicking one will close a different one ..
Kevin
@Kevin - Doh, your structure's a bit different, you probably want `.next(".logic-holder")` instead of siblings...just depends on where you place the +/- element.
Nick Craver
Is that supposed to be a .next?
Kevin
@Kevin -Yup, it selects the next sibling *if* it matches the selector...I can't tell you exactly what to use because I have no idea where your +- element is in relation to what it's toggling, my point was just find it relatively from `$(this)` using the tree traversal methods: http://api.jquery.com/category/traversing/tree-traversal/
Nick Craver
Ok, so I've got this
Kevin
$(".login-holder").click(function() { $(this).next(".login-holder").slideToggle(); }); })But it only toggles on the first div
Kevin
@Kevin - I would ask a new question with your markup for this...too hard to make out in comments.
Nick Craver
Hi Nick - I've made a new question ... think you can help out?http://stackoverflow.com/questions/3859072/multiple-slidetoggle-one-function
Kevin