1

I'm trying to a define my own function in jQuery to condense my js file, but I seem to be unable to shorten these two lines. Below is:

*My function

*What it looks like with the function

*What the original code looks like without the function

var AddContent = function(content) {
    $(#log li:last).remove();
    $(#log).append(content);
};


    $('.object').click(function(){
        AddContent('<li>content1</li>');
    });



    $('.object').click(function(){
        $('#log li:last').remove();
        $('#log').append('<li>content1</li>');
    });

What am I doing wrong? Help!

1
  • 1
    need quotes around your selectors Commented Jul 12, 2011 at 17:47

1 Answer 1

2

In your new function AddContent -- you need to put quote around the selectors.

I.e.

var AddContent = function(content) {
    $(#log li:last).remove();
    $(#log).append(content);
};

Should be

var AddContent = function(content) {
    $('#log li:last').remove();
    $('#log').append(content);
};
Sign up to request clarification or add additional context in comments.

2 Comments

Also, you may want to tryout the replaceWith function instead of remove/add api.jquery.com/replaceWith
Your welcome -- You could also just "Accept Answer" instead of leaving a comment :-)

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.