Richland College Multimedia Learning Center

JavaScript, jQuery, and React
Home e-Handouts and Assignments Syllabus Resources Student Sites Other Classes RLC Multimedia
Updated 5/4/2025

jQuery with HTML and CSS

References


jQuery with HTML

jQuery has some very handy functions that you can use to change HTML tags and attributes.

Some of the "HTML"-related jQuery functions are these:


An .html() Example

Here is an example of how the .html() method changes the contents (innerHTML) of HTML elements that you select with jQuery selectors. This is page jQueryHTML.html:

<!doctype html>
<html>
<head>
  <meta charset="UTF-8">
  <link rel="stylesheet" type="text/css" href="Samples.css">
  <title>Example of jQuery html() function</title>
  <script type="text/javascript" src="jquery.js"></script>
  <script type="text/javascript">
  $(document).ready(function(){
    $("button").click(function(){
      $("p").html("This paragraph's content was replaced by the html() function.");
    });
  });
  </script>
</head>

<body>
  <div id="mainDiv">
    <h2>Example of jQuery html() Function</h2>
    <p>This is a paragraph.</p>
    <p>This is another paragraph.</p>
    <button>Click me</button>
  </div>
</body>
</html>
Please note these points about the above code:



Another .html() Example

Here is another example of .html() actions with jQuery. This is page jQueryMystified.html:

<!doctype html>
<html>
<head>
  <meta charset="UTF-8">
  <link rel="stylesheet" type="text/css" href="Samples.css">
  <title>Some jQuery Fun</title>
  <script type="text/javascript" src="jquery.js"></script>
  <script type="text/javascript">
    /* <![CDATA[ */
    $(document).ready(function(){
      $("button").click(
        function()
        {
          $("#secondP").html("<strong>Good grief, Charlie Brown! What's going on here?</strong>");
          $("button").html("Double-click me for the answer");
        }
      );
      
      $("button").dblclick(
        function()
        {
          $("#thirdP").html("<strong>We're playing football in the snow!</strong>");
          $("button").hide();
        }
      );
    });
    /* ]]> */
  </script>
  <style type="text/css">
    body
    {
      font-family: Arial,sans-serif;
    }
  </style>
</head>

<body>
  <div id="mainDiv">
    <h2>This is a heading</h2>
    <p>This is a paragraph.</p>
    <p id="secondP">This is another paragraph.</p>
    <p id="thirdP"></p>
    <button>Click me</button>
  </div>
</body>
</html>

Please note these points about the above code:



An .attr() Example

In this sample code, the .attr() jQuery function will be used to change the HTML attribute of a tag.

Specifically, this sample page will disable a form's submit button.

Now here is the code. This is page jqueryAttrFunction.html:

<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <link rel="stylesheet" type="text/css" href="Samples.css">
  <title>Change an HTML attribute with the attr() function</title>
  <script src="jquery.js"></script>
  <script>
    $(document).ready(function(){
      $("#wombat").click(function(){
        $("#wombat").attr("disabled", true);
        $("#womForm").submit();
      });
    });
  </script>
</head>

<body>
  <div id="mainDiv">
    <form id="womForm" method="get" action="http://www.jimlink.net/PHPStuff/TestFormDataDelay.php">
      Your name:  <input type="text" id="yourName" name="yourName" autofocus>
      <br><br>
      <input type="submit" id="wombat" value="Submit the form">
    </form>
  </div>
</body>
</html>

Please note these points about the above code:


jQuery with CSS

jQuery has one main function that you can use to change the CSS properties of selected elements. This function is css().

The css() function has three different syntaxes, which you use to accomplish different CSS-related tasks. These syntaxes are:

Also, you can use the following two functions to directly change the width or height of a selected element:


.css() to Get a Property

Here is an example of how you can use the css() function to find the value of a CSS property of a selected element. This is page jQueryCSSFunction.html:

<!doctype html>
<html>
<head>
  <meta charset="UTF-8">
  <link rel="stylesheet" type="text/css" href="Samples.css">
  <title>Get a CSS property value with the css() function</title>
  <script type="text/javascript" src="jquery.js"></script>
  <script type="text/javascript">
    $(document).ready(function(){
      $("#box").click(function(){
        $("#firstPara").html("The background color of the box is " + $(this).css("background-color") + ".");
      });
    });
  </script>
</head>

<body>
  <div id="mainDiv">
    <div id="box" style="width:100px;height:100px;background:#ff0000"></div>
    <p id="firstPara">Click in the square box to return the background color.</p>
  </div>
</body>
</html>

Please note these points about the above code:


.css() to Set Properties

And now here is an example of how you can change multiple CSS property values with the css() function. This is page jQueryMultipleCSS.html:

<!doctype html>
<html>
<head>
  <meta charset="UTF-8">
  <link rel="stylesheet" type="text/css" href="Samples.css">
  <title>Multiple CSS Property Changes with jQuery css() function</title>
  <script type="text/javascript" src="jquery.js"></script>
  <script type="text/javascript">
  $(document).ready(function(){
    $("button").click(function(){
      $("p").css({"background-color":"yellow", "font-size":"200%", "border":"1px solid blue", "border-radius":"10px", "padding":"10px"});
    });
  });
</script>
</head>

<body>
  <div id="mainDiv">
    <h2>This is a heading</h2>
    <p>This is a paragraph.</p>
    <p>This is another paragraph.</p>
    <button>Click me</button>
  </div>
</body>
</html>

Please note these points about the above code:



A Combined Example

Here is an example which combines several of the functions that we looked at earlier in this e-handout. This is page CombinedQueryExample2.html:

<!doctype html>
<html>
<head>
  <meta charset="UTF-8">
  <link rel="stylesheet" type="text/css" href="Samples.css">
  <title>A Combined Example</title>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <script type="text/javascript">
    /* <![CDATA[ */
    $(document).ready(function()
    {
      $("#para1").on("click", function()
      {
        $("#para4").text($(this).attr("label"));
        $("#para1").fadeOut(1000, function()
                                 {
                                   $("#para4").fadeIn(2000).delay(2000).fadeOut(2000, function()
                                                                                       {
                                                                                         $("#para2").fadeIn(1000);
                                                                                       });
                                 });
        
        
      });

      $("#para2").on("click", function()
      {
        $("#para4").text($(this).attr("label"));
        $("#para2").fadeOut(1000, function()
                                 {
                                   $("#para4").fadeIn(2000).delay(2000).fadeOut(2000, function()
                                                                                       {
                                                                                         $("#para3").fadeIn(1000);
                                                                                       });
                                 });
        
        
      });

      $("#para3").on("click", function()
      {
        $("#para4").text($(this).attr("label"));
        $("#para3").fadeOut(1000, function()
                                 {
                                   $("#para4").fadeIn(2000).delay(2000).fadeOut(1000);
                                 });
        
        
      });
  });
    /* ]]> */
  </script>
  <style type="text/css">
    body
    {
      font-family: Arial,sans-serif;
    }
    
    #para2
    {
      display: none;
    }
    
    #para3
    {
      display: none;
    }
    
    #para4
    {
      display: none;
      font-style: italic;
    }
    
    #mainDiv
    {
      height: 450px;
    }
    
    #diary
    {
      height: 280px;
    }
    
    .citation
    {
      text-decoration: underline;
      font-style: italic;
    }
    
    .citeP
    {
      font-size: 0.7em;
    }
  </style>
</head>

<body>
  <div id="mainDiv">
    <div id="diary">
      <h2>A Mysterious Diary</h2>
      <p id="para1" label="Hello, Harry Potter. My name is Tom Riddle. How did you come by my diary?">My name is Harry Potter.</p>
      <p id="para2" label="Lucky that I recorded my memories in some more lasting way than ink.">Someone tried to flush it down a toilet.</p>
      <p id="para3" label="I can show you, if you like...  I can take you inside my memory...">It's happening again now.  There have been three attacks...  Who was it last time?</p>
      <p id="para4"></p>
    </div>
    <div id="bottomStuff">
      <h4>(Click the top (non-italic) paragraph three times, for a surprise each time.  WAIT for the surprise to disappear before you click again.)</h4>
      <p class="citeP">Text used here is from <span class="citation">Harry Potter and the Chamber of Secrets</span> by J.K. Rowling.</p>
    </div>
  </div>
</body>
</html>

Please note these points about the above code: