devxlogo

Create an HTML Element and Hold it in a Reference Variable

Create an HTML Element and Hold it in a Reference Variable

When you create a new HTML element, you may want to keep its reference and later add a few more properties to it in the script. Just store it in a var, and continue accessing it. See below for an example.

var myDiv = $("
Dummy Content
");…../*Later access the jQuery object with the var name and continue working with the object*/myDiv.attr("id", "newlyCreatedDev");myDiv.attr("class", "div mClass").appendTo("body");
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist