Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • jQuery Tutorial
  • jQuery Selectors
  • jQuery Events
  • jQuery Effects
  • jQuery Traversing
  • jQuery HTML & CSS
  • jQuery AJAX
  • jQuery Properties
  • jQuery Examples
  • jQuery Interview Questions
  • jQuery Plugins
  • jQuery Cheat Sheet
  • jQuery UI
  • jQuery Mobile
  • jQWidgets
  • Easy UI
  • Web Technology
Open In App
Next Article:
How to create To-Do List using jQuery?
Next article icon

How to create a div using jQuery with style tag ?

Last Updated : 28 Apr, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

Creating an <div> element with a style tag using jQuery can be done in the following steps.

Steps:

  • Create a new <div> element.
  • Create an object with all the styles that we want to apply.
  • Choose a parent element, where to put this newly created element.
  • Put the created div element into the parent element.

Example 1: This example will create an <div> element and use the append() method to append the element at the end of the parent element.

HTML
<!DOCTYPE html> <html>  <head>     <script src="https://code.jquery.com/jquery-git.js">     </script>      <style>         .divClass {             height: 40px;             width: 200px;             border: 1px solid blue;             color: white         }     </style> </head>  <body>     <h2 style="color:green">GeeksforGeeks</h2>     <div id="parent">         This is parent div     </div>     <div style="height:10px;"></div>     <!-- JavaScript function addText() is called to add to parent div-->     <input type="button" value="Add <div> Text" onclick="addText()" />     <script>         <!-- Function to add text in a div element with above styles-->         function addText() {             $(document).ready(function() {                  var object = {                     id: "divID",                     class: "divClass",                     css: {                         "color": "Red"                     }                 };                 var addition = $("<div>", object);                 addition.html("Added text GFG");                 $("#parent").append(addition);             });         }     </script> </body>  </html> 

Output:

append

Example 2: This example will create an <div> element and uses the prependTo() method to append the element at the start of the parent element.

HTML
<!DOCTYPE html> <html>  <head>     <script src="https://code.jquery.com/jquery-git.js">     </script>      <style>         .divClass {             height: 40px;             width: 200px;             border: 1px solid blue;             color: white         }     </style> </head>  <body>     <h2 style="color:green">GeeksforGeeks</h2>     <div style="height:10px;"></div>     <div id='parent'>         This is parent div     </div>     <div style="height:10px;"></div>     <input type="button"             value="Prepend text div  "             onclick="prependDiv()" />     <script>         function prependDiv() {             $(document).ready(function() {                 var object = {                     id: "divID",                     class: "divClass",                     css: {                         "color": "Red"                     }                 };         <!--Prepend object is created before the parent div-->                 var prependObject = $("<div>", object);                 prependObject.html("Prepend text is GFG");                 $(prependObject).prependTo($('#parent'));             });         }     </script> </body>  </html> 

Output:

Prepend text

Next Article
How to create To-Do List using jQuery?

K

KapilChhipa
Improve
Article Tags :
  • Web Technologies
  • JQuery
  • jQuery-Methods
  • jQuery-Questions

Similar Reads

  • How to create To-Do List using jQuery?
    This article focuses on developing a To-do List with some basic features like: Add Task Delete Task Cross the completed tasks Prerequisites: Basic knowledge of Front-end development using HTML, CSS, JS, jQuery & Bootstrap-3. Steps: Initialize the layout: - Add a input text-box with a button to a
    4 min read
  • How to Create a Style Tag using JavaScript?
    To create a <style> tag using JavaScript, use the document.createElement() method to create the tag, which allows you to dynamically add elements to the DOM. Syntax document.createElement('style')Example 1: This HTML code dynamically styles a <div> element with a green background and whi
    1 min read
  • How to set border width using jQuery ?
    In this article, we will see how to set the border width using jQuery. To set the border width using jQuery, we will use css() method. This method is used to change the style property of the selected element. Syntax: $(selector).css(property, value) // Or $(selector).css({property: value, property:
    2 min read
  • How to create a news ticker using jQuery ?
    A news ticker is a text-based display either in the form of a graphic that displays news scrolling text running from up to down or right to left. In this article, we will use a jQuery plugin called easy-ticker to create a news ticker in a simple manner. To use this plugin, simply add the CDN link in
    3 min read
  • How to create a simple map using jQuery ?
    jQuery is an open-source JavaScript library that simplifies the interactions between an HTML/CSS document, or more precisely the Document Object Model (DOM), and JavaScript. What are Google Maps? Google Map is a free web-based mapping service application and technology provided by Google. The applic
    4 min read
  • How to apply CSS style using jQuery ?
    In this article, we will explore how we can apply CSS styles to HTML elements. It is recommended that you have some basic knowledge of HTML, CSS, JavaScript, and jQuery before beginning this article. It is possible to change the CSS property of an element by using a simple JavaScript API, but we try
    2 min read
  • How to set attribute without value using jQuery ?
    There are many ways to set attributes without value using jQuery. jQuery prop() method is used to get or set the values of attributes. To change the properties like checked, default checked, disabled, selectedIndex, default selected and selected state of Document Object Model, use the jQuery .prop()
    3 min read
  • How to get styles of a clicked division using jQuery ?
    In this article, we will learn how to get the styles (width, height, text color, and background color) of a clicked division using jQuery. Approach: The css() method is used to get the CSS property values of the selected element in jQuery. We use the css() method in jQuery that takes an array of nam
    2 min read
  • How to Wrap an Element with Another Div Using jQuery?
    We will see how to wrap an element with another div in jQuery. We will simply wrap an element inside a div tag in jQuery. These are the following approaches: Table of Content Using wrap() method Using wrap.inner() methodApproach 1: Using wrap() method We include the jQuery library in the <head
    3 min read
  • How to animate div width and height on mouse hover using jQuery ?
    In order to animate the div width and height on mouse hover, we can use the jQuery animate() method along with the mouseenter() and mouseleave() methods. .animate() method: The animate() method changes the state of the element with CSS style. Syntax: $(selector).animate({styles}, para1, para2, para3
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences