Reading/Writing to/from HTML Elements via jQuery

Introduction: This tutorial is on how to read and write to/from HTML elements via jQuery. HTML: Before we can read/write to our HTML via jQuery, we first need our HTML...
  1.         <head>
  2.  
  3.         </head>
  4.         <body>
  5.                 <div id='myDiv'>Here is some text.</div>
  6.         </body>
  7. </html>
So now we have a basic division with the ID of 'myDiv' which we will use in our jQuery in a second. jQuery API: Now we need to include the jQuery libary files before we can begin using jQuery. We will simply get the latest API links from the official jQuery website (http://jquery.com/download/). Add these in the 'head' tags of your HTML file...
  1. <script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
  2. <script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
jQuery Ready: Now we are ready to write our jQuery/Javascript. First we create our 'script' tags to put our javascript in. Again, this goes in the 'head' tags of your HTML file... Within our 'script' tags, we first create our document ready (shorthand) function...
  1. $(function() {
  2.        
  3. });
The above function automatically runs once the page is fully loaded, this avoids us having to create separate listeners and functions in order for me to explain the main purpose of this tutorial. Variables: Now, within our jQuery 'document ready (shorthand)' function, we are going to first of all declare our string that we are wanting to write to our div later. I call the variable 'content' for ease of use purposes...
  1. var content = 'Content written.';
Now we are going to read our div's contents. First we make a reference to our div using it's ID 'myDiv'...
  1. var readContents = $('#myDiv')
After our element, we use dot notation (when you use a dot (.) followed by a function or property of the preceeding element, object or variable) to access the 'innerHTML' property of the HTML element. This will set our new 'readContents' variable's value to whatever text and other elements are within our first div with the id of 'myDiv'...
  1. var readContents = $('#myDiv').innerHTML;
Now we have read the contents of our 'myDiv' (ID) division to our new 'readContents' variable. Next we are going to write to the div - before that, let's just alert (messagebox/pop-up) the 'readContents' variable to ensure that it is gathering the information correctly...
  1. alert(readContents);
jQuery Write: To write to an element via jQuery, we can use the exact same property, but set it instead of just getting it instead. Here is an example;
  1. $('#myDiv').innerHTML = contents;
The above code would set the 'innerHTML' property (the contents) of the first division with the ID of 'myDiv' to our earlier defined variable 'content's value (I set it to 'Content written.'). Finished! Here is the full source code...
  1.         <head>
  2.                 <script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
  3.                 <script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
  4.                 <script>
  5.                         $(function() {
  6.                                 var contents = 'Content written.';
  7.                                 var readContents = $('#myDiv').innerHTML;
  8.                                 alert(readContents);
  9.                                 $('#myDiv').innerHTML = contents;
  10.                         });
  11.                 </script>
  12.         </head>
  13.         <body>
  14.                 <div id='myDiv'>Here is some text.</div>
  15.         </body>
  16. </html>

Add new comment