0

So, as you can see in the code below I want to create an HTML element, actually a div with class but since I need to create a div like this: <div class="btn btn-danger">Close</div>, this is not working for me. Here is the code:

close: function () {
                var $this = $(this);

                $('<div class=btn>Close</div>')
                    .appendTo('p')
                    .on('click', function () {
                        $this.slideUp();
                    });
            }

Thanks for your answers!

2
  • 2
    $('<div class="btn">Close</div>') -> the html attributes must be used between quotes Commented Aug 30, 2014 at 20:02
  • possible duplicate of JQuery Adding multiple classes to a div tag Commented Aug 30, 2014 at 20:36

2 Answers 2

3

That 's the correct line:

$('<div class="btn btn-danger">Close</div>')

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

Comments

1

another way to do this :

$(this).append($('<div />').attr({
        'id': idVariable,
        'class': classVariable
    }));

but now this snippet is reusable, you can put an id and any classes you want.

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.