Santhosh Yedidi Santhosh Yedidi - 6 months ago 9
jQuery Question

In Jquery how to refer javascript's element created by document.createElement

I have a code already written in javascript:

var objTo = document.getElementById('krishna')
var href1 = document.createElement("div");
href1.setAttribute("style","float: left");
objTo.appendChild(href1);


Already so many elements are appended to href1 in javascript.

I want to add some more elements to href1 using jquery

I tried
href1.append("<br>");


it says
href1.append is not a function


How to use jquery append to append an element to href1.

Answer

You need to turn the native element into a jQuery object. Native elements don't recognize jQuery methods

Try

$(href1).append("<br>");

If you want to do all this with jQuery alone the code is quite simple

// create new <div> element and apply style
var href1 = $("<div>").css('float','left');
// append to id=krishna
$('#krishna').append(href1);