-3

I am trying to inserting div in Jquery, like in the example below. It doesn't work.

Jquery:

$('<div />', {
    $('<img />', { "src": "/Pages/Images/calendar.png").addClass('image').appendTo($div);
    $('<input/>', { "type": "text", "class": "ctb" }).addClass('ctb').appendTo($div);
    }).addClass('sto').appendTo($div);

The output I want is like the image below:

Image

Demo

AdiT
  • 539
  • 5
  • 18

3 Answers3

1

You didn't create $(div) yet. You should do it like

var div = $('<div></div>');
$(div).append(...);

Btw, the best method to create element is

document.createElement();

jQuery document.createElement equivalent?

Community
  • 1
  • 1
Baseleus
  • 142
  • 8
0

You can't append the elements like that you must do this

$('<div>', {
    "class": "sto"
}).append('<img />', {
    "src": "/Pages/Images/calendar.png",
    "class": "image"
}).append('<input/>', {
    "type": "text",
    "class": "ctb"
}).appendTo($div);
Anton
  • 32,245
  • 5
  • 44
  • 54
  • I have test it, works fine. But the problem is that a input field appears on the card. I don't want it. I want it like the image above. What am I able to do, to fix it? I have a demo with your part of code: http://jsfiddle.net/62QY8/46/ – AdiT Mar 25 '14 at 13:51
0

There were a lot of syntax errors.

I changed you part to:

$('<div />').append($('<img src="/Pages/Images/calendar.png" class="image" />')).appendTo($div);
$('<input type="text" class="ctb sto" />').appendTo($div);

Demo:http://jsfiddle.net/62QY8/45/

Amit Joki
  • 58,320
  • 7
  • 77
  • 95