Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript How can i simplify my ugly jQuery? (code included lol)

  • This topic is empty.
Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #38133
    kgscott284
    Participant

    Essentially I am just trying to avoid using duplicate code over and over, I am trying to eliminate the need to do the following for each page on the site…I honestly don’t HAVE to, but i really want to learn how.


    $('.contentWrap').hide();
    $('.navigation #home').click(function(e) {
    e.preventDefault();
    // some code here to hide the page if it is open...
    $('.contentWrap').load('home.html', function() {
    $(this).fadeIn('slow');
    });
    });​
    $('.navigation #about').click(function(e) {
    e.preventDefault();
    // some code here to hide the page if it is open...
    $('.contentWrap').load('about.html', function() {
    $(this).fadeIn('slow');
    });
    });​

    .....and so on and so on...

    Thanks guys, i really wanna fix that garbage.

    #103088
    Johnnyb
    Member

    Couldn’t you just grab the ID from whatever button’s clicked like so…

    $('.navigation a').click(function(e) {
    e.preventDefault();
    var $self = $(this);
    // some code here to hide the page if it is open...
    $('.contentWrap').load($self.attr('id') + '.html', function() {
    $(this).fadeIn('slow');
    });
    });​

    So long as the anchor id matches the html file name then it should load in that file. I prefer Karl’s method though so that you can link to that page directly.

Viewing 2 posts - 1 through 2 (of 2 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.