Results 1 to 8 of 8

Thread: Can i make my code shorter?

  1. #1
    Join Date
    Nov 2016
    Posts
    23
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Can i make my code shorter?

    I have a code with links to different pages like this.

    Javascript

    Code:
    <script type="text/javascript">
        
    $(document).on("click",".item", function(e) {
      var $deel = $(this).text();  
       if ( $deel == "Statuten"){  
         $('#uitkomst').load('statuten.php');
       }
    else if ( $deel == "Boekhouding"){  
         $('#uitkomst').load('boekhouding.php');
       }
        
    else if ( $deel == "Jaarrekening"){  
         $('#uitkomst').load('jaarrekening.php');
       }
        
    else if ( $deel == "Begroting"){  
         $('#uitkomst').load('begroting.php');
       } 
        
    else if ( $deel == "Budget opvolgen"){  
         $('#uitkomst').load('budgetopvolgen.php');
       }    
       e.preventDefault();
    });
    I have about 34 of those links and I wonder if there's a way to make my code shorter?

  2. #2
    Join Date
    Nov 2014
    Location
    On A Scottish Island
    Posts
    488
    Thanks
    0
    Thanked 62 Times in 58 Posts

    Default

    Have a look here.

  3. The Following User Says Thank You to styxlawyer For This Useful Post:

    Bicklo (12-04-2016)

  4. #3
    Join Date
    Nov 2016
    Posts
    23
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default

    Hi, thank you for the link,

    This is what I got now
    Code:
    <script type="text/javascript">
        
    $(document).on("click",".item", function(e) {
      var $deel = $(this).text();
        
        
    switch($deel){    
        case "Statuten":  
         $('#uitkomst').load('statuten.php');
       break;
            
        case "Boekhouding":  
         $('#uitkomst').load('boekhouding.php');
       break;
            
     case "Jaarrekening":  
         $('#uitkomst').load('jaarrekening.php');
       break;
        
    case "Begroting":  
         $('#uitkomst').load('begroting.php');
       break;
        
    case "Budget opvolgen":  
         $('#uitkomst').load('budgetopvolgen.php');
       break;
    
       }    
       e.preventDefault();
    });  
          
     
     
    </script> 
    
    I don't think it can't get any shorter than that.
    
    Thank you for your help.

  5. #4
    Join Date
    Sep 2007
    Location
    The Netherlands
    Posts
    1,879
    Thanks
    49
    Thanked 266 Times in 258 Posts
    Blog Entries
    56

    Default

    You can reduce your code to just a few lines of code
    Code:
    <script type="text/javascript">
    $(document).on("click",".item", function(e) {
    $('#uitkomst').load($(this).text()+'.php');
    e.preventDefault();
    });
    </script>
    provided you make sure that the text for your links is identical to the name of the php-file you want to load except for the php-extension.

    So <a class="item" >statuten</a> would load statuten.php, <a class="item" >boekhouding</a> would load boekhouding.php etc.

  6. The Following User Says Thank You to molendijk For This Useful Post:

    Bicklo (12-06-2016)

  7. #5
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Or if you want to use upper case letters in the text, but all lower case filenames:

    Code:
    <script type="text/javascript">
    $(document).on("click",".item", function(e) {
    $('#uitkomst').load($(this).text().toLowerCase() + '.php');
    e.preventDefault();
    });
    </script>
    But I'm thinking that using this inside the load function might not work. If not, then:

    Code:
    <script type="text/javascript">
    $(document).on("click",".item", function(e) {
    var filename = $(this).text().toLowerCase() + '.php';
    $('#uitkomst').load(filename);
    e.preventDefault();
    });
    </script>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  8. The Following User Says Thank You to jscheuer1 For This Useful Post:

    Bicklo (12-06-2016)

  9. #6
    Join Date
    Nov 2016
    Posts
    23
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default

    Hi molendijk, this is wonderful it saves me lots of lines.

    jscheuer1, I think .toLowerCase() will come in handy later in my code.

    Thank you very much gentlemen.

  10. #7
    Join Date
    Nov 2014
    Location
    On A Scottish Island
    Posts
    488
    Thanks
    0
    Thanked 62 Times in 58 Posts

    Default

    You will also need to remove the space from this case:

    Code:
    case "Budget opvolgen":  
         $('#uitkomst').load('budgetopvolgen.php');

  11. The Following User Says Thank You to styxlawyer For This Useful Post:

    Bicklo (12-06-2016)

  12. #8
    Join Date
    Nov 2016
    Posts
    23
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default

    I saw this earlier and made the appropriate changes, thank you anyway.

Similar Threads

  1. Want to make my code more efficient
    By chrisdab in forum JavaScript
    Replies: 1
    Last Post: 12-31-2008, 12:40 AM
  2. Shorten your PHP Code, and make it easy.
    By Nile in forum Coding tips & tutorials threads
    Replies: 6
    Last Post: 06-26-2008, 07:23 PM
  3. Replies: 1
    Last Post: 12-22-2006, 03:42 PM
  4. how do you make your own video code?
    By Quanlin127 in forum HTML
    Replies: 3
    Last Post: 12-20-2006, 05:47 AM
  5. Can anyone make this code work in FF and IE7?
    By Koster in forum JavaScript
    Replies: 0
    Last Post: 12-11-2006, 05:11 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •