Comments in jQuery are used to create notes about what the code block does and how it works, and can also be used to temporarily prevent code execution.
Single Line Comments
Any text between the // and the end of the line will be ignored by the browser (will not be executed). Anything on the line before the // will run normally. And anything on lines after the comment will run normally.
$('button').click(function() {
$('p').hide();
// $('p').show();
});
document.getElementById('my_output').innerHTML = 'Hello World';
Multi-line Comments
Comments in jQuery can also wrap to multiple lines and anything between the /* and */ will be the comment, including any code that you don’t want the browser to run. This is called “commenting out” code.
/* this is a
a much longer
comment
$('p').hide();
*/
As mentioned above, comments in jQuery can also be used to “comment out” a section of code that you’d like to keep intact for future use, but “turn off” for the time being. By wrapping a comment around the code, it will not be implemented by the browser.
The structure of the comment stays the same, but the comment itself is the code you no longer want the browser to run. In the following, we comment out the original string and replace it with “Hello Universe”.
document.getElementById('my_output').innerHTML = /* 'Hello World' */ 'Hello Universe';
document.getElementById('my_output').innerHTML /* = 'Hello World' */ 'Hello Universe';
We’d like to acknowledge that we learned a great deal of our coding from W3Schools and TutorialsPoint, borrowing heavily from their teaching process and excellent code examples. We highly recommend both sites to deepen your experience, and further your coding journey. We’re just hitting the basics here at 1SMARTchicken.
Why 1SMARTchicken?
See More →
Thanks for your support!
jQuery Books
All are affiliate links. Thank you for your support!
jQUERY NOTES:
- To use jQuery on your site, it must first be downloaded from the official jQuery site and linked to in your document
head
, or linked to via a CDN in your documenthead
- It is generally good practice to place your jQuery code/function inside the document load function so that the action takes place ONLY after the document has finished loading
- When using jQuery, single or double quotation marks are acceptable and work identically to one another; choose whichever you prefer, and stay consistent
Feedback
If you see an error on the page or the code itself is incorrect or incomplete, or just plain wrong, please let us know. We’re always learning. NOTE: we do not sell your information and will not send you spam emails.