mcbeav
mcbeav

Reputation: 12275

Any way to turn this into a jQuery function?

I have tried to wrap this in a function and cannot get it to work. I am wondering if it is because it is appending something to the body? But here is the script. Any help is greatly appreciated.

$('.showIt').click(function(){
$('body').append("<div class='show'></div>");
});

I would like to put that into a function i can call, but i cant seem to get it to work, This is what i have tried and it fails to work for me.

function create(){
$('body').append("<div class='show'></div>");
});
$('.showIt').click(function(){
create():
});

can someone please help?

Upvotes: 0

Views: 53

Answers (3)

jondavidjohn
jondavidjohn

Reputation: 62392

function create(){
    $('body').append("<div class='show'></div>");
}

$('.showIt').click(function(){
    create();
});

after cleaning up your code a bit I don't know why you can't do this.

you did have an extra ); after closing your create() function though.

Upvotes: 1

Greg
Greg

Reputation: 1426

This will add a div.show to body when you'll click on a link/element with class .showIt

function create(){
$('body').append("<div class='show'></div>");
}

$('.showIt').click(function(){
create();
});

In your code you had a });when you should have } and : when ; should be Is that what you're looking for?

Upvotes: 2

SLaks
SLaks

Reputation: 887469

You have an extra );.
Get rid of it.

Your first function is written as function create(){ ... });

Upvotes: 4

Related Questions