0

I am having trouble using some javascript that I am trying to convert from inline to an external javascript document. This code, that I'm trying to place externally does not work.

// JavaScript Document
function homepage() {
    $("#button").click(function(){
        $("#main, #nav").slideToggle();
        if($(this).html() == "-"){
            $(this).html("+");
            setTimeout(function() {
                $('#wrapper').css('top', 'auto');
                $('#wrapper').animate({ 'bottom': '0' }, 500);
            }, 500);
        } else {
            $(this).html("-");
            setTimeout(function() {
                $('#wrapper').animate({ 'top': '0' }, 500);
                $('#wrapper').css('bottom', 'auto');
            }, 500);
        }
    });
}
4
  • 2
    What doesn't work exactly? Commented Jun 14, 2013 at 15:15
  • Doesn't work "correctly" or doesn't work at all? Commented Jun 14, 2013 at 15:16
  • Where do you call this homepage() functionm? Commented Jun 14, 2013 at 15:17
  • jsfiddle.net/HGCaF/6 this is the code that I am using, thanks for the quick response guys. Commented Jun 14, 2013 at 15:24

3 Answers 3

1

Tries to lock up the script so:

$(document).ready(function(){
  // JavaScript Document
  function homepage() {
  $("#button").click(function(){
  $("#main, #nav").slideToggle();
  if($(this).html() == "-"){
      $(this).html("+");
      setTimeout(function() {
        $('#wrapper').css('top', 'auto');
        $('#wrapper').animate({ 'bottom': '0' }, 500);
      }, 500);
  }
  else{
      $(this).html("-");

      setTimeout(function() {
          $('#wrapper').animate({ 'top': '0' }, 500);
          $('#wrapper').css('bottom', 'auto');
      }, 500);
  }
  });}

});
Sign up to request clarification or add additional context in comments.

1 Comment

not sure why but it's still not functioning, it's a little bit strange.
0

I assume that it "doesn't work" because your jQuery selectors are failing. Put your code inside as $(function () {...}) to insure it runs on DOM-ready.

Comments

0

Your code makes me assume that you are using jQuery. To move a JS file that uses a library like jQuery to an external file, you have to do the following steps:

If the code is inside jQuery and looks like this:

$(document).ready(function(){
    // here is all your code
});

Then you have to move all of it, including the $(document).ready(); part into the external file and (best) load it at the bottom of your html page.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.