I'm new to jQuery and I've been messing about with this code, It works but I want to learn how to shorten the code by the eliminating unnecessary repeated code.
Below is a link to jsFiddle
http://jsfiddle.net/eSZFD/168/
Thanks for reading.
HTML
<div id="div1" style="display:none;">one<div id="divb" class="upper">close</div></div>
<div id="div2" style="display:none;">two<div id="divb" class="upper">close</div></div>
<div id="div3" style="display:none;">three<div id="divb" class="upper">close</div></div>
<div id="div4" style="display:none;">four<div id="divb" class="upper">close</div></div>
</br></br></br></br></br></br></br></br></br></br></br><div class="buttons">
<a class="button" id="showdiv1">Div 1</a>
<a class="button" id="showdiv2">Div 2</a>
<a class="button" id="showdiv3">Div 3</a>
<a class="button" id="showdiv4">Div 4</a>
</div>
JQuery
$(function() {
$('#showdiv1').click(function() {
$('html, body').animate({ scrollTop: 0 }, 'slow');
$('div[id^=div]').slideUp().delay(1000);
$('#div1').slideDown('slow').delay(1000);
});
$('.upper').click(function() {
$('html, body').animate({ scrollTop: 0 }, 'slow');
$('#div1').slideUp('slow');
});
$('#showdiv2').click(function() {
$('html, body').animate({ scrollTop: 0 }, 'slow');
$('div[id^=div]').slideUp().delay(1000);
$('#div2').slideDown('slow').delay(1000);
});
$('.upper').click(function() {
$('html, body').animate({ scrollTop: 0 }, 'slow');
$('#div2').slideUp('slow');
});
$('#showdiv3').click(function() {
$('html, body').animate({ scrollTop: 0 }, 'slow');
$('div[id^=div]').slideUp().delay(1000);
$('#div3').slideDown('slow').delay(1000);
});
$('.upper').click(function() {
$('html, body').animate({ scrollTop: 0 }, 'slow');
$('#div3').slideUp('slow');
});
$('#showdiv4').click(function() {
$('html, body').animate({ scrollTop: 0 }, 'slow');
$('div[id^=div]').slideUp().delay(1000);
$('#div4').slideDown('slow').delay(1000);
});
$('.upper').click(function() {
$('html, body').animate({ scrollTop: 0 }, 'slow');
$('#div4').slideUp('slow');
});
})
Ok, after much headache - I'm new to this sh#t. I've changed the code to this. All works fine. I would like someone that knows, to give me any tips for improvement, as I'm totally improvising here, thankyou.
jquery
$(document).ready(function(){
$('a').click(function () {
var divname= this.name;
$('html, body').animate({ scrollTop: 0 }, 'slow');
$('div[id^=div]').slideUp().delay(1000);
$("#"+divname).slideDown('slow').delay(1000);
});
$('.upper').click(function() {
$('html, body').animate({ scrollTop: 0 }, 'slow');
$('div[id^=div]').slideUp('slow');
});
});
html
<body>
<div id="div1" style="display:none">
Hello World <a class="upper" id="div1">close</a>
</div>
<div id="div2" style="display:none">
Test<a class="upper" id="div2">close</a>
</div>
<div id="div3" style="display:none">
Another Test
</div>
<div id="div4" style="display:none">
Final Test
</div>
</br></br></br></br></br></br></br></br></br></br></br></br></br></br></br></br></br></br></br></br></br></br></br></br></br></br></br></br></br></br></br></br></br></br></br></br></br></br></br></br></br></br></br></br></br></br></br>
<div class="menu">
<ul>
<li><a href="#" name="div1" >one</a></li>
<li><a href="#" name="div2" >two</a></li>
<li><a href="#" name="div3" >three</a></li>
<li><a href="#" name="div4" >four</a></li>
</ul>
</div>
</body>