Wednesday 23 October 2019

How can I convert a DOM element to a jQuery element?




I am creating an element with document.createElement().
Now how can I pass it to a function that only takes a Jquery object?



$("#id") 


I can not use it, as the element has not been rendered in the page yet.


Answer



var elm = document.createElement("div");
var jelm = $(elm);//convert to jQuery Element

var htmlElm = jelm[0];//convert to HTML Element

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...