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 add CSS properties to an element dynamically using jQuery ?
Next article icon

How to add `style=display:“block”` to an element using jQuery?

Last Updated : 19 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

To add the inline style style=”display: block” to HTML elements using jQuery, there are several approaches, each with its advantages. Below are the different methods you can use:

Table of Content

  • Using the CSS() method
  • Using the show() method
  • Using the attr() method
  • Using the prop() method

Using the CSS() method

The css() method allows you to directly apply CSS properties to selected elements by passing the property name and value as arguments.

Syntax:

$('element_selector').css('property_name', 'value');

Example: The below example will illustrate the use of the css() method to add the style=”display: block” to an element using jQuery.

html
<!DOCTYPE html>  <html lang="en">   <head>      <meta charset="utf-8">      <title>          How to add `style=display:“block”`to          an element using jQuery?      </title>      <style>          p {              color: green;              font-weight: bold;              cursor: pointer;          }      </style>      <script src= "https://code.jquery.com/jquery-1.10.2.js">      </script>  </head>   <body style="text-align:center;">      <h1 style="color:green;">          GeeksForGeeks      </h1>      <h2> Using .css()</h2>      <div>          <p style="display: none">             It is displayed using the              style="display: block"              property added using css() method.         </p>          <button>click to display</button>     </div>      <script>          $('button').click(()=>{              $("p").css("display", "block");          });     </script>   </body>   </html>  

Output:

dispBlk

Using the show() method

The show() method is a shorthand way of setting the display property to block. It’s specifically designed for making elements visible.

Syntax:

$('element_selector').show();

Example: The below example will illustrate the use of the show() method to add the style=”display: block” to an element using jQuery.

html
<!DOCTYPE html>  <html lang="en">   <head>      <meta charset="utf-8">      <title>          How to add `style=display:“block”`to          an element using jQuery?      </title>      <style>          p {              color: green;              font-weight: bold;              cursor: pointer;          }      </style>      <script src= "https://code.jquery.com/jquery-1.10.2.js">      </script>  </head>   <body style="text-align:center;">      <h1 style="color:green;">          GeeksForGeeks      </h1>      <h2> How to add `style=display:“block”`to          an element using jQuery? </h2>      <div>          <p style="display: none">             It is displayed using the              style="display: block"              property added using css() method.         </p>          <button>click to display</button>     </div>      <script>          $('button').click(()=>{              $("p").show();          });     </script>   </body>   </html>  

Output:

dispBlk

Using the attr() method

The attr() method can be used to set any attribute on an HTML element, including inline styles.

Syntax:

$('element_selector').attr('attribute', 'value'); 

Example: The below example will illustrate the use of the attr() method to add the style=”display: block” to an element using jQuery.

html
<!DOCTYPE html>  <html lang="en">   <head>      <meta charset="utf-8">      <title>          How to add `style=display:“block”`to          an element using jQuery?      </title>      <style>          p {              color: green;              font-weight: bold;              cursor: pointer;          }      </style>      <script src= "https://code.jquery.com/jquery-1.10.2.js">      </script>  </head>   <body style="text-align:center;">      <h1 style="color:green;">          GeeksForGeeks      </h1>      <h2> How to add `style=display:“block”`to          an element using jQuery? </h2>      <div>          <p style="display: none">             It is displayed using the              style="display: block"              property added using css() method.         </p>          <button>click to display</button>     </div>      <script>          $('button').click(()=>{              $("p").attr('style', 'display: block');          });     </script>   </body>   </html>  

Output:

dispBlk

Using the prop() method

The prop() method used to set the style=”display: block” by passing the parameters in the same way as we did in the last example with the attr() method.

Syntax:

$('element_selector').prop('property', 'value'); 

Example: The below example will illustrate the use of the prop() method to add the style=”display: block” to an element using jQuery.

HTML
<!DOCTYPE html>  <html lang="en">   <head>      <meta charset="utf-8">      <title>          How to add `style=display:“block”`to          an element using jQuery?      </title>      <style>          p {              color: green;              font-weight: bold;              cursor: pointer;          }      </style>      <script src= "https://code.jquery.com/jquery-1.10.2.js">      </script>  </head>   <body style="text-align:center;">      <h1 style="color:green;">          GeeksForGeeks      </h1>      <h2> How to add `style=display:“block”`to          an element using jQuery? </h2>      <div>          <p style="display: none">             It is displayed using the              style="display: block"              property added using css() method.         </p>          <button>click to display</button>     </div>      <script>          $('button').click(()=>{              $("p").prop('style', 'display: block');          });     </script>   </body>   </html>  

Output:

dispBlk

jQuery is an open source JavaScript library that simplifies the interactions between an HTML/CSS document, It is widely famous with it’s philosophy of “Write less, do more”. You can learn jQuery from the ground up by following this jQuery Tutorial and jQuery Examples.



Next Article
How to add CSS properties to an element dynamically using jQuery ?

S

SHUBHAMSINGH10
Improve
Article Tags :
  • JQuery
  • Web Technologies
  • jQuery-Misc

Similar Reads

  • How to add CSS properties to an element dynamically using jQuery ?
    In this article, we will see how to add some CSS properties dynamically using jQuery. To add CSS properties dynamically, we use css() method. The css() method is used to change the style property of the selected element. The css() method can be used in different ways. This method can also be used to
    1 min read
  • How to apply styles on an element using jQuery ?
    In this article, we will learn how one can apply a style on an element using jQuery. Suppose you are building a website or game where you need the user interface to be interactive. In such cases styles can play a huge role there in this post we are going to learn how can we change styles when certai
    3 min read
  • How to add and remove CSS classes to an element using jQuery ?
    In this article, we will see how to add or remove the CSS classes to an element using jQuery. To add the CSS classes to an element we use addClass() method, and to remove the CSS classes we use removeClass() method. Syntax: The syntax for adding CSS classes to an element: $('selector').addClass(clas
    2 min read
  • How to apply css property to a child element using JQuery?
    The task is to apply CSS property to a child element using jQuery. To do that, first we select the child element with the help of children() method in jQuery and then apply CSS property to it with the help of css() method in jQuery. Syntax: // Note : children method selects the direct child to paren
    2 min read
  • How to add options to a select element using jQuery?
    We will dynamically add the options to the select element using jQuery. jQuery has a lot of in-built methods. But, we will use some of them to accomplish the purpose of dynamically adding the options to the select element as listed below: Table of Content By passing the HTML of the option tag to the
    4 min read
  • How to set background color for an elements using jQuery ?
    In this article, we will set the background color for an element using jQuery. To add the background color, we use css() method. The css() method in JQuery is used to change style property of the selected element. The css() in JQuery can be used in different ways. The css() method can be used to che
    1 min read
  • How to add a class to the last paragraph element using jQuery ?
    In this article, we will learn to add a class to the last paragraph element using jQuery. To add a class to the last paragraph element, we use last() and addClass() methods. The jQuery last() function is an inbuilt function that is used to find the last element of the specified elements. Syntax: $(s
    1 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 Display Element on Hover using CSS ?
    In web development, displaying an element on hover is a common interaction pattern used to reveal additional content or actions. Below are the various CSS approaches to achieve this effect: Table of Content Using the display propertyUsing the visibility propertyUsing the display propertyThis approac
    2 min read
  • How to click a button to animate the paragraph element using jQuery ?
    In this article, we will click a button to animate the paragraph element using jQuery. To animate the paragraph elements, we use animate() method. The animate() method is used to change the state of the element with CSS style. This method can also be used to change the CSS property to create the ani
    1 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