Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

jQuery question - Show one DIV out of many by default when page loads ? 1

Status
Not open for further replies.

c0deM0nK424

Programmer
Oct 28, 2007
126
GB
okay this is how far i've managed to get - i've embedded a jQuery script on this website using basekit sitebuilder, inside 'page scripts'.

instead of opting for multiple sub-menu links, my client and I agreed it would be better if we housed all that information as show and hide divs under one main navigation link - so that on that page, you can see them all.


My question is this - using my code, what could you add to make the FIRST DIV #services1 SHOW on default when the page loads ?


here is the jQuery script - its a long winded approach yes, i will refine it in time when i get better at jQuery but for now it works.

<script src="<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">$(document).ready(function(){
$("#services1").css('cursor', 'pointer');
$("#LifestyleManagement").hide();
$("#HouseholdManagement").hide();
$("#Networking").hide();
$("#InvestmentAdvice").hide();
$("#OfficeServices").hide();
$("#MeetingsAndVisitingClients").hide();
$("#TicketsAndEvents").hide();
$("#Relocation").hide();
$("#VisitingUKLondon").hide();
$("#TravelAssistance").hide();
$("#ChildrensEducation").hide();
$("#HealthServices").hide();
$("#Property").hide();
$("#services1").click(function(){
$("#LifestyleManagement").slideToggle("slow");
$("#HouseholdManagement").hide();
$("#Networking").hide();
$("#InvestmentAdvice").hide();
$("#OfficeServices").hide();
$("#MeetingsAndVisitingClients").hide();
$("#TicketsAndEvents").hide();
$("#Relocation").hide();
$("#VisitingUKLondon").hide();
$("#TravelAssistance").hide();
$("#ChildrensEducation").hide();
$("#HealthServices").hide();
$("#Property").hide();
});
});$(document).ready(function(){
$("#services2").css('cursor', 'pointer');
$("#LifestyleManagement").hide();
$("#HouseholdManagement").hide();
$("#Networking").hide();
$("#InvestmentAdvice").hide();
$("#OfficeServices").hide();
$("#MeetingsAndVisitingClients").hide();
$("#TicketsAndEvents").hide();
$("#Relocation").hide();
$("#VisitingUKLondon").hide();
$("#TravelAssistance").hide();
$("#ChildrensEducation").hide();
$("#HealthServices").hide();
$("#Property").hide();
$("#services2").click(function(){
$("#HouseholdManagement").slideToggle("slow");
$("#LifestyleManagement").hide();
$("#Networking").hide();
$("#InvestmentAdvice").hide();
$("#OfficeServices").hide();
$("#MeetingsAndVisitingClients").hide();
$("#TicketsAndEvents").hide();
$("#Relocation").hide();
$("#VisitingUKLondon").hide();
$("#TravelAssistance").hide();
$("#ChildrensEducation").hide();
$("#HealthServices").hide();
$("#Property").hide();
});
});$(document).ready(function(){
$("#services3").css('cursor', 'pointer');
$("#LifestyleManagement").hide();
$("#HouseholdManagement").hide();
$("#Networking").hide();
$("#InvestmentAdvice").hide();
$("#OfficeServices").hide();
$("#MeetingsAndVisitingClients").hide();
$("#TicketsAndEvents").hide();
$("#Relocation").hide();
$("#VisitingUKLondon").hide();
$("#TravelAssistance").hide();
$("#ChildrensEducation").hide();
$("#HealthServices").hide();
$("#Property").hide();
$("#services3").click(function(){
$("#Networking").slideToggle("slow");
$("#LifestyleManagement").hide();
$("#HouseholdManagement").hide();
$("#InvestmentAdvice").hide();
$("#OfficeServices").hide();
$("#MeetingsAndVisitingClients").hide();
$("#TicketsAndEvents").hide();
$("#Relocation").hide();
$("#VisitingUKLondon").hide();
$("#TravelAssistance").hide();
$("#ChildrensEducation").hide();
$("#HealthServices").hide();
$("#Property").hide();
});
});$(document).ready(function(){
$("#services4").css('cursor', 'pointer');
$("#LifestyleManagement").hide();
$("#HouseholdManagement").hide();
$("#Networking").hide();
$("#InvestmentAdvice").hide();
$("#OfficeServices").hide();
$("#MeetingsAndVisitingClients").hide();
$("#TicketsAndEvents").hide();
$("#Relocation").hide();
$("#TravelAssistance").hide();
$("#ChildrensEducation").hide();
$("#HealthServices").hide();
$("#Property").hide();
$("#services4").click(function(){
$("#InvestmentAdvice").slideToggle("slow");
$("#LifestyleManagement").hide();
$("#HouseholdManagement").hide();
$("#Networking").hide();
$("#OfficeServices").hide();
$("#MeetingsAndVisitingClients").hide();
$("#TicketsAndEvents").hide();
$("#Relocation").hide();
$("#VisitingUKLondon").hide();
$("#TravelAssistance").hide();
$("#ChildrensEducation").hide();
$("#HealthServices").hide();
$("#Property").hide();
});
});$(document).ready(function(){
$("#services5").css('cursor', 'pointer');
$("#LifestyleManagement").hide();
$("#HouseholdManagement").hide();
$("#Networking").hide();
$("#InvestmentAdvice").hide();
$("#OfficeServices").hide();
$("#MeetingsAndVisitingClients").hide();
$("#TicketsAndEvents").hide();
$("#Relocation").hide();
$("#VisitingUKLondon").hide();
$("#TravelAssistance").hide();
$("#ChildrensEducation").hide();
$("#HealthServices").hide();
$("#Property").hide();
$("#services5").click(function(){
$("#OfficeServices").slideToggle("slow");
$("#LifestyleManagement").hide();
$("#HouseholdManagement").hide();
$("#Networking").hide();
$("#InvestmentAdvice").hide();
$("#MeetingsAndVisitingClients").hide();
$("#TicketsAndEvents").hide();
$("#Relocation").hide();
$("#VisitingUKLondon").hide();
$("#TravelAssistance").hide();
$("#ChildrensEducation").hide();
$("#HealthServices").hide();
$("#Property").hide();
});
});$(document).ready(function(){
$("#services6").css('cursor', 'pointer');
$("#LifestyleManagement").hide();
$("#HouseholdManagement").hide();
$("#Networking").hide();
$("#InvestmentAdvice").hide();
$("#OfficeServices").hide();
$("#MeetingsAndVisitingClients").hide();
$("#TicketsAndEvents").hide();
$("#Relocation").hide();
$("#VisitingUKLondon").hide();
$("#TravelAssistance").hide();
$("#ChildrensEducation").hide();
$("#HealthServices").hide();
$("#Property").hide();
$("#services6").click(function(){
$("#MeetingsAndVisitingClients").slideToggle("slow");
$("#LifestyleManagement").hide();
$("#HouseholdManagement").hide();
$("#Networking").hide();
$("#InvestmentAdvice").hide();
$("#OfficeServices").hide();
$("#TicketsAndEvents").hide();
$("#Relocation").hide();
$("#VisitingUKLondon").hide();
$("#TravelAssistance").hide();
$("#ChildrensEducation").hide();
$("#HealthServices").hide();
$("#Property").hide();
});
});$(document).ready(function(){
$("#services7").css('cursor', 'pointer');
$("#LifestyleManagement").hide();
$("#HouseholdManagement").hide();
$("#Networking").hide();
$("#InvestmentAdvice").hide();
$("#OfficeServices").hide();
$("#MeetingsAndVisitingClients").hide();
$("#TicketsAndEvents").hide();
$("#Relocation").hide();
$("#VisitingUKLondon").hide();
$("#TravelAssistance").hide();
$("#ChildrensEducation").hide();
$("#HealthServices").hide();
$("#Property").hide();
$("#services7").click(function(){
$("#TicketsAndEvents").slideToggle("slow");
$("#LifestyleManagement").hide();
$("#LifestyleManagement").hide();
$("#HouseholdManagement").hide();
$("#Networking").hide();
$("#InvestmentAdvice").hide();
$("#OfficeServices").hide();
$("#MeetingsAndVisitingClients").hide();
$("#Relocation").hide();
$("#VisitingUKLondon").hide();
$("#TravelAssistance").hide();
$("#ChildrensEducation").hide();
$("#HealthServices").hide();
$("#Property").hide();
});
});$(document).ready(function(){
$("#services8").css('cursor', 'pointer');
$("#LifestyleManagement").hide();
$("#HouseholdManagement").hide();
$("#Networking").hide();
$("#InvestmentAdvice").hide();
$("#OfficeServices").hide();
$("#MeetingsAndVisitingClients").hide();
$("#TicketsAndEvents").hide();
$("#Relocation").hide();
$("#VisitingUKLondon").hide();
$("#TravelAssistance").hide();
$("#ChildrensEducation").hide();
$("#HealthServices").hide();
$("#Property").hide();
$("#services8").click(function(){
$("#Relocation").slideToggle("slow");
$("#LifestyleManagement").hide();
$("#HouseholdManagement").hide();
$("#Networking").hide();
$("#InvestmentAdvice").hide();
$("#OfficeServices").hide();
$("#MeetingsAndVisitingClients").hide();
$("#TicketsAndEvents").hide();
$("#TravelAssistance").hide();
$("#VisitingUKLondon").hide();
$("#ChildrensEducation").hide();
$("#HealthServices").hide();
$("#Property").hide();
});
});$(document).ready(function(){
$("#services9").css('cursor', 'pointer');
$("#LifestyleManagement").hide();
$("#HouseholdManagement").hide();
$("#Networking").hide();
$("#InvestmentAdvice").hide();
$("#OfficeServices").hide();
$("#MeetingsAndVisitingClients").hide();
$("#TicketsAndEvents").hide();
$("#Relocation").hide();
$("#VisitingUKLondon").hide();
$("#TravelAssistance").hide();
$("#ChildrensEducation").hide();
$("#HealthServices").hide();
$("#Property").hide();
$("#services9").click(function(){
$("#VisitingUKLondon").slideToggle("slow");
$("#LifestyleManagement").hide();
$("#HouseholdManagement").hide();
$("#Networking").hide();
$("#InvestmentAdvice").hide();
$("#OfficeServices").hide();
$("#MeetingsAndVisitingClients").hide();
$("#TicketsAndEvents").hide();
$("#Relocation").hide();
$("#TravelAssistance").hide();
$("#ChildrensEducation").hide();
$("#HealthServices").hide();
$("#Property").hide();
});
});$(document).ready(function(){
$("#services10").css('cursor', 'pointer');
$("#LifestyleManagement").hide();
$("#HouseholdManagement").hide();
$("#Networking").hide();
$("#InvestmentAdvice").hide();
$("#OfficeServices").hide();
$("#MeetingsAndVisitingClients").hide();
$("#TicketsAndEvents").hide();
$("#Relocation").hide();
$("#VisitingUKLondon").hide();
$("#TravelAssistance").hide();
$("#ChildrensEducation").hide();
$("#HealthServices").hide();
$("#Property").hide();
$("#services10").click(function(){
$("#TravelAssistance").slideToggle("slow");
$("#LifestyleManagement").hide();
$("#HouseholdManagement").hide();
$("#Networking").hide();
$("#InvestmentAdvice").hide();
$("#OfficeServices").hide();
$("#MeetingsAndVisitingClients").hide();
$("#TicketsAndEvents").hide();
$("#Relocation").hide();
$("#VisitingUKLondon").hide();
$("#ChildrensEducation").hide();
$("#Property").hide();
});
});$(document).ready(function(){
$("#services11").css('cursor', 'pointer');
$("#LifestyleManagement").hide();
$("#HouseholdManagement").hide();
$("#Networking").hide();
$("#InvestmentAdvice").hide();
$("#OfficeServices").hide();
$("#MeetingsAndVisitingClients").hide();
$("#TicketsAndEvents").hide();
$("#Relocation").hide();
$("#VisitingUKLondon").hide();
$("#TravelAssistance").hide();
$("#ChildrensEducation").hide();
$("#HealthServices").hide();
$("#Property").hide();
$("#services11").click(function(){
$("#ChildrensEducation").slideToggle("slow");
$("#LifestyleManagement").hide();
$("#HouseholdManagement").hide();
$("#Networking").hide();
$("#InvestmentAdvice").hide();
$("#OfficeServices").hide();
$("#MeetingsAndVisitingClients").hide();
$("#TicketsAndEvents").hide();
$("#Relocation").hide();
$("#VisitingUKLondon").hide();
$("#TravelAssistance").hide();
$("#HealthServices").hide();
$("#Property").hide();
});
});$(document).ready(function(){
$("#services12").css('cursor', 'pointer');
$("#LifestyleManagement").hide();
$("#HouseholdManagement").hide();
$("#Networking").hide();
$("#InvestmentAdvice").hide();
$("#OfficeServices").hide();
$("#MeetingsAndVisitingClients").hide();
$("#TicketsAndEvents").hide();
$("#Relocation").hide();
$("#VisitingUKLondon").hide();
$("#TravelAssistance").hide();
$("#ChildrensEducation").hide();
$("#HealthServices").hide();
$("#Property").hide();
$("#services12").click(function(){
$("#HealthServices").slideToggle("slow");
$("#LifestyleManagement").hide();
$("#HouseholdManagement").hide();
$("#Networking").hide();
$("#InvestmentAdvice").hide();
$("#OfficeServices").hide();
$("#MeetingsAndVisitingClients").hide();
$("#TicketsAndEvents").hide();
$("#Relocation").hide();
$("#VisitingUKLondon").hide();
$("#TravelAssistance").hide();
$("#ChildrensEducation").hide();
$("#Property").hide();
});
});$(document).ready(function(){
$("#services13").css('cursor', 'pointer');
$("#LifestyleManagement").hide();
$("#HouseholdManagement").hide();
$("#Networking").hide();
$("#InvestmentAdvice").hide();
$("#OfficeServices").hide();
$("#MeetingsAndVisitingClients").hide();
$("#TicketsAndEvents").hide();
$("#Relocation").hide();
$("#VisitingUKLondon").hide();
$("#TravelAssistance").hide();
$("#ChildrensEducation").hide();
$("#HealthServices").hide();
$("#Property").hide();
$("#services13").click(function(){
$("#Property").slideToggle("slow");
$("#LifestyleManagement").hide();
$("#HouseholdManagement").hide();
$("#Networking").hide();
$("#InvestmentAdvice").hide();
$("#OfficeServices").hide();
$("#MeetingsAndVisitingClients").hide();
$("#TicketsAndEvents").hide();
$("#Relocation").hide();
$("#VisitingUKLondon").hide();
$("#TravelAssistance").hide();
$("#ChildrensEducation").hide();
$("#HealthServices").hide();
});
});</script>


The HTML markup is irrelevant at this point , and im sure so is the css. But if u want to view the css here is that as well:

#services1
{
float: left;
margin-left: 1em;
color: black;
font-size: 16;
font-weight: bold;
text-align: center;
text-shadow: 0.1em 0.1em 0.1em white;
}

#services2
{
float: left;
margin-left: 1em;
color: black;
font-size: 16;
font-weight: bold;
text-align: center;
text-shadow: 0.1em 0.1em 0.1em white;
}

#services3
{
float: left;
margin-left: 1em;
color: black;
font-size: 16;
font-weight: bold;
text-align: center;
text-shadow: 0.1em 0.1em 0.1em white;
}

#services4
{
float: left;
margin-left: 1em;
color: black;
font-size: 16;
font-weight: bold;
text-align: center;
text-shadow: 0.1em 0.1em 0.1em white;
}

#services5
{
float: left;
margin-left: 1em;
color: black;
font-size: 16;
font-weight: bold;
text-align: center;
text-shadow: 0.1em 0.1em 0.1em white;
}

#services6
{
float: left;
margin-left: 1em;
color: black;
font-size: 16;
font-weight: bold;
text-align: center;
text-shadow: 0.1em 0.1em 0.1em white;
}

#services7
{
float: left;
margin-left: 1em;
color: black;
font-size: 16;
font-weight: bold;
text-align: center;
text-shadow: 0.1em 0.1em 0.1em white;
}

#services8
{
float: left;
margin-left: 1em;
color: black;
font-size: 16;
font-weight: bold;
text-align: center;
text-shadow: 0.1em 0.1em 0.1em white;
}

#services9
{
float: left;
margin-left: 1em;
color: black;
font-size: 16;
font-weight: bold;
text-align: center;
text-shadow: 0.1em 0.1em 0.1em white;
}

#services10
{
float: left;
margin-left: 1em;
color: black;
font-size: 16;
font-weight: bold;
text-align: center;
text-shadow: 0.1em 0.1em 0.1em white;
}

#services11
{
float: left;
margin-left: 1em;
color: black;
font-size: 16;
font-weight: bold;
text-align: center;
text-shadow: 0.1em 0.1em 0.1em white;
}

#services12
{
float: left;
margin-left: 1em;
color: black;
font-size: 16;
font-weight: bold;
text-align: center;
text-shadow: 0.1em 0.1em 0.1em white;
}

#services13
{
float: left;
margin-left: 1em;
color: black;
font-size: 16;
font-weight: bold;
text-align: center;
text-shadow: 0.1em 0.1em 0.1em white;
}

#LifestyleManagement
{
width: 90%;
padding-top: 1em;
margin-left: 2em;
height: auto;
font-size: 12px;
display: none;
}

#HouseholdManagement
{
width: 90%;
padding-top: 1em;
margin-left: 2em;
height: auto;
font-size: 12px;
display: none;
}

#Networking
{
width: 90%;
padding-top: 1em;
margin-left: 2em;
height: auto;
font-size: 12px;
display: none;
}

#InvestmentAdvice
{
width: 90%;
padding-top: 1em;
margin-left: 2em;
height: auto;
font-size: 12px;
display: none;
}

#OfficeServices
{
width: 90%;
padding-top: 1em;
margin-left: 2em;
height: auto;
font-size: 12px;
display: none;
}

#MeetingsAndVisitingClients
{
width: 90%;
padding-top: 1em;
margin-left: 2em;
height: auto;
font-size: 12px;
display: none;
}

#TicketsAndEvents
{
width: 90%;
padding-top: 1em;
margin-left: 2em;
height: auto;
font-size: 12px;
display: none;
}

#TicketsAndEvents
{
width: 90%;
padding-top: 1em;
margin-left: 2em;
height: auto;
font-size: 12px;
display: none;
}

#Relocation
{
width: 90%;
padding-top: 1em;
margin-left: 2em;
height: auto;
font-size: 12px;
display: none;
}

#VisitingUKLondon
{
width: 90%;
padding-top: 1em;
margin-left: 2em;
height: auto;
font-size: 12px;
display: none;
}

#TravelAssistance
{
width: 90%;
padding-top: 1em;
margin-left: 2em;
height: auto;
font-size: 12px;
display: none;
}

#ChildrensEducation
{
width: 90%;
padding-top: 1em;
margin-left: 2em;
height: auto;
font-size: 12px;
display: none;
}

#HealthServices
{
width: 90%;
padding-top: 1em;
margin-left: 2em;
height: auto;
font-size: 12px;
display: none;
}

#Property
{
width: 90%;
padding-top: 1em;
margin-left: 2em;
height: auto;
font-size: 12px;
display: none;
}



So what do i add in the script, what do i change slightly - in order for the first one to appear on the page by default ?

i want the content under services1 (Lifestyle Management) to 'show' when the page 'personal concierge' loads.

Any tips advice ?
 
i'd use a tried and tested accordion plugin, were I you.

also i'd use a common class rather than explicitly hide/show each id.

but to answer your specific question add this to the end of script

Code:
$(document).ready(function(){
 $('#services1').trigger('click');
});
 
how do you define/declare a common class ? NM i think I know how to do that ...and thanks for the response jpadie.

You're responses are invaluable to us all. thanks.
 
just add class="accordion" to each accordion title then use some markup like this
Code:
<div class="accordion">
 <div class="accordionTitle">Title</div>
 <div class="accordionContent">...</div>
</div>

you could also use dl, dt and dd tags or whatever.

then a very basic accordion type script would look like this

Code:
$(document).ready(function(){
 $('.accordionContent').not(':first').hide(); //hides each content set other than the first
 
 $(document).on('click', '.accordionTitle', function(e){
   e.stopPropagation(); //stop the click propagating. 
   var cont = $(this).closest('.accordion').find('.accordionContent');
   if(cont.is(':visible')){ 
     cont.hide();
   }else{
     $('.accordionContent').hide();
     cont.show('slow');
   }
});
});

the javascript could be simpler (using parent() and sibling() etc but this way guarantees that it won't break if you insert other complementary mark up.

hopefully you can see that the above is rather easier to consume, extend and manage than iterating each of the ids each time. As the click is on the document, the code will also survive more accordion panels being dynamically inserted, if that's the way you want to go. If not you could safely change the line to
Code:
$('.accordionTitle').on('click', function(e){ ...
this may have better compatibility (I recall running into problems recently with trapping document level events with one of the mobile browsers. don't remember which, I'm afraid - and it could well have been an artefact of jQuery mobile rather than the browser itself.

instead of the hide(), show() you could animate a scroll effect if you really want to load jquery ui (1 MB or so) to do so. or you could just load an animation plugin or whatever. I'm not convinced of their respective UI value, tbh...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top