-3

Trying to call a function, by testing it I tried to do 'alert' but perhaps 1.11 doesn't support that but it's not working even when I test it in things that I know work. So doing a simple $('body').hide() in the function to see if it's called, and it's not for some reason. Probably syntax error, new to web programming. Any help is appreciated. My guess is that it's not in the document ready function, so if that's correct how do i work around it?

$(document).ready(function() {
    $('a.show').click(function(){
        testFunc();
    }); //closes click
}); //closes ready

function testFunc()
{
    $('body').hide();
});
Huangism
  • 16,278
  • 7
  • 48
  • 74
  • 1
    the `testFunc` has the redundant `)` at the end, is that a typo? – King King May 28 '14 at 18:34
  • If the javascript was removed, what would you expect the anchor tag to do? does it have `href=""`, which would cause it to reload the page? – Kevin B May 28 '14 at 18:37

2 Answers2

0

You need to remove the ); after the testFunc definition.

function testFunc()
{
    $('body').hide();
}

See Fiddle

For future reference, when debugging javascript the first place to look is the console.


You may also need to prevent the default linking action for the anchor with preventDefault();.

$(document).ready(function() {
    $('a.show').click(function(event){
        event.preventDefault();
        testFunc();
    }); //closes click
}); //closes ready
Community
  • 1
  • 1
Derek
  • 3,438
  • 1
  • 17
  • 35
  • wow makes sense, however it's not hiding body when I test it. edit works thanks – user3624298 May 28 '14 at 18:36
  • 1
    Please check your console or post all of the Javascript code. What may be happening is another piece of JS code is erroring out and preventing any other JS execution. – Kodlee Yin May 28 '14 at 18:39
  • nope I thought I needed a ); at end but I didn't. New to jquery so I knew it had to be a small syntax error, thanks again. – user3624298 May 28 '14 at 18:40
-1

if you dont provide any error details i assume that you should put it in that order :

function testFunc()
{
    $('body').hide();
};
$(document).ready(function() {
    $('a.show').click(function(){
        testFunc();
    }); //closes click
}); //closes ready
john Smith
  • 17,409
  • 11
  • 76
  • 117