I have a jquery code to distinct between two divs
$("#addresslink").click(function() {
$("#main").find("div").each(function(n,i){
var id = this.id;
if(id == "addressdiv")
{
$('#' + id).fadeIn(300);
}
else
{
$('#' + id).fadeOut(250);
}
});
});
$("#storylink").click(function() {
$("#main").find("div").each(function(n,i){
var id = this.id;
if(id == "storydiv")
{
$('#' + id).fadeIn(300);
}
else
{
$('#' + id).fadeOut(250);
}
});
});
is there a better way to tie a to a div rather than hardcoding each to a then showing and hiding the other divs?
P.S: I have done the code just asking whether could i optimize this?
One way to optimize without changing anything is move the common code to function:
OR
you can also do this :