-1

I am making a site and this code lets me have several pages in 1 .html file. Can the iterative-ness of this code be avoided? Can it be simplified?

<script type="text/javascript">
        var x = 300;
        $(function() { 
            $("#indexLink").click(function() { 
                $("#content>div").not("#start").hide(x);
                $("#index").show(x);
                $('nav>ul>li').removeClass('active');
                $('#indexLink').addClass('active');
            });

            $("#leerlingLink").click(function() { 
                $("#content>div").not("#start").hide(x); 
                $("#leerling").show(x); 
                $('nav>ul>li').removeClass('active');
                $('#leerlingLink').addClass('active');
            });

            $("#bestemmingLink").click(function() { 
                $("#content>div").not("#start").hide(x); 
                $("#bestemming").show(x);
                $('nav>ul>li').removeClass('active');
                $('#bestemmingLink').addClass('active');
            });

            $("#betalingLink").click(function() { 
                $("#content>div").not("#start").hide(x); 
                $("#betaling").show(x);
                $('nav>ul>li').removeClass('active');
                $('#betalingLink').addClass('active');
            });
        });

</script>
Cœur
  • 37,241
  • 25
  • 195
  • 267
Skullyhoofd
  • 51
  • 1
  • 7
  • 4
    Please show the corresponding html. Chances are by using a class you can have one function that will work for all scenarios. – Jared Feb 05 '13 at 19:41
  • Yes, please show your HTML. What is #content? – showdev Feb 05 '13 at 19:43
  • Have you made any attempt to consolidate it into one handler? – Kevin B Feb 05 '13 at 19:44
  • You probably want to change the title of your question. You are trying to keep your code DRY, generally when your talking about programming `iterative` refers to a loop. – Ryan Feb 05 '13 at 19:44
  • @user2044453 Did you delete your original code? Possible to get it back? It was useful for others to find solutions to similar problems. – showdev Feb 07 '13 at 04:17

3 Answers3

1

Assuming that the things being clicked are something other than anchor links you could give the elements all the same class and write this once -

EDIT: made a change based on the information that these are anchor links.

$('.classname').click(function(event) {
    event.preventDefault(); 
    $("#content>div").not("#start").hide(x);
    var idString = this.id
    var shortIDString = idString.slice(0,-4); //remove the 'Link'
    $("#" + shortIDString).show(x);
    $('nav>ul>li').removeClass('active');
    $(this).addClass('active');
});
Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
  • They are anchor links, this shows the one you click and hides the rest so it looks like multiple pages but actually it is 1 page. (Sorry English is not my main language!) – Skullyhoofd Feb 05 '13 at 20:03
  • If they are anchor links you'd still want to add a class to them and make the change that I just added. – Jay Blanchard Feb 05 '13 at 20:05
  • This is the way to go, so +1, but too many variables IMO. `$("#" + this.id.slice(0,-4)).show(x);` – the system Feb 05 '13 at 20:13
  • Thanks @thesystem, I just ran it together quickly and showed the logic for the OP – Jay Blanchard Feb 05 '13 at 20:19
  • This unfortunaly didnt work but Nicos's solution did. Still thanks for trying to help though! – Skullyhoofd Feb 05 '13 at 20:23
  • 1
    @user2044453: If it "didn't work", then you didn't implement the answer as described. Probably didn't bother giving a class to your elements. – the system Feb 05 '13 at 21:19
0

You can use a for loop and construct the selectors:

$(function () {
    var items = ["index", "leerling", "bestemming", "betaling"];
    for (var i = 0; i < items.length; i++) {
        var item = items[i];
        $("#" + item + "Link").click(function () {
            $("#content>div").not("#start").hide(x);
            $("#" + item).show(x);
            $('nav>ul>li').removeClass('active');
            $('#' + item + 'Link').addClass('active');
        });
    }
});

You could also add a handler to all #whateverLinks and then detect the link name based on the id value of the event target.

Matt Zeunert
  • 16,075
  • 6
  • 52
  • 78
  • This looks like something that could work but it doesn't work with this. I have posted my code for more info for you guys! English is not my main language, sorry! pastebin.com/xHmZTQSe HTML pastebin.com/VWUvNYSq CSS – Skullyhoofd Feb 05 '13 at 20:04
  • Could you create a JS fiddle with the code that gets returned from the server? What specifically doesn't work? – Matt Zeunert Feb 05 '13 at 20:07
0

what do you mean by iterative-ness ?

if you want your code to be DRY you could just write somthing like this:

<script type="text/javascript">
    var x = 300;
    $(function () {
        $("#indexLink,#betalingLink,#bestemmingLink,#leerlingLink").click(function () {
            $("#content>div").not("#start").hide(x);
            $("#"+$(this).attr("id").slice(0,-4)).show(x);
            $('nav>ul>li').removeClass('active');
            $('#indexLink').addClass('active');
        });
    });
</script>

this will take all selectors, one at the time, and make all of them run that code of yours, basically the same as you typed, but in one selector only

EDIT

Fixed div showing

EDIT2

added jsfiddle to show it working: http://jsfiddle.net/NicosKaralis/Vurjf/

Nicos Karalis
  • 3,724
  • 4
  • 33
  • 62