{"id":603,"date":"2025-02-14T16:14:49","date_gmt":"2025-02-14T08:14:49","guid":{"rendered":"https:\/\/witlab.ph\/blog\/?p=603"},"modified":"2025-02-14T16:14:50","modified_gmt":"2025-02-14T08:14:50","slug":"useful-functions-of-javascript-and-jquery","status":"publish","type":"post","link":"https:\/\/witlab.ph\/blog\/useful-functions-of-javascript-and-jquery\/","title":{"rendered":"Useful Functions of JavaScript And JQuery"},"content":{"rendered":"\n<p>Hi, good day to our valued blog readers! My name is Htay Min Khaung, and I work at WIT Lab Co., Ltd. Today, I would like to share how to use some useful JavaScript functions in web app projects. JavaScript is one of the most powerful languages in the web development environment. JQuery is also one of the most useful library in the web development. jQuery offers a wide range of powerful functions and built-in animations to help achieve customer requirements efficiently.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">How to add customize function to the String prototype ?<\/h4>\n\n\n\n<p>If you want to customize the String prototype with a function similar to .toLowerCase(), you can add it as follows:<\/p>\n\n\n\n<pre class=\"brush:powershell\">\nString.prototype.capitalizeEachWord = function(){\n     return this.replace(\/\\b\\w\/g, char => char.toUpperCase());\n}\n\nString.prototype.replaceUnderscoreWithSpace = function(){\n      return this.split('_').join(' ');\n}\n<\/pre>\n\n\n\n<p>The first function will be added to the String prototype and can be used to capitalize each word in a sentence. The second function will replace all underscores (_) with spaces (&#8216; &#8216;). After defining them, you can easily call these functions on any string using the dot (.) notation.<\/p>\n\n\n\n<p><strong>How to add Classes to the DOM using JavaScript ?<\/strong><\/p>\n\n\n\n<p>const ele = document.querySelector(&#8216;#app&#8217;);<\/p>\n\n\n\n<p>In the above code, element with id (app) is selected.<\/p>\n\n\n\n<pre class=\"brush:powershell\">\nele.className += ' color-red border-yellow bg-green';\nele.classList.add(...['color-red', 'border-yellow', 'bg-green']);\nele.setAttribute('class', 'color-red border-yellow bg-green');\n<\/pre>\n\n\n\n<p>There are three ways to add a class name to the DOM. The first is by string concatenation, the second is by adding a value to an array, and the third is by setting the class attribute with a string.<\/p>\n\n\n\n<p><strong>How to clear classes from the DOM ?<\/strong><\/p>\n\n\n\n<pre class=\"brush:powershell\">\nele.className = '';\nele.classList = [];\nele.setAttribute('class', '');\n<\/pre>\n\n\n\n<p>You can remove class names from the DOM using the same three methods: clearing the string, emptying the array, or setting the class attribute to an empty string.<\/p>\n\n\n\n<p>Next is how to use $.fn in jQuery ?<\/p>\n\n\n\n<p>In jQuery, you can use $.fn, which is <strong>an alias for jQuery<\/strong>. This feature is particularly useful for controlling DOM elements and managing all child elements under a parent, especially in dynamic web applications like chat apps. For jQuery CDN, you can get from this link <a href=\"https:\/\/releases.jquery.com\/\">https:\/\/releases.jquery.com\/<\/a><\/p>\n\n\n\n<pre class=\"brush:powershell\">\n<div id=\"app\">\n        <p>Hello Word<\/p>\n    <\/div>\n    <button onclick=\"bx.textRed()\">Text Red<\/button>\n    <button onclick=\"bx.bgGreen()\">Background Green<\/button>\n    <button onclick=\"bx.borderYellow()\">Border Yellow<\/button>\n    <script>\n\n        $.fn.box = function() {\n\n            this.textRed = function(){\n                this.addClass('text-red');\n                return this;\n            }\n\n            this.borderYellow = function(){\n                this.addClass('border-yellow');\n                return this;\n            }\n\n            this.bgGreen = function(){\n                this.addClass('bg-green');\n                return this;\n            }\n\n            return this;\n        }\n\n        var bx = $('#app').box();\n        bx.textRed().borderYellow().bgGreen();\n\n    <\/script>\n<\/pre>\n\n\n\n<p>In this function, an element with the ID &#8216;app&#8217; is created and controlled by &#8216;bx&#8217;. You can add custom functions to $.fn.box and call them from outside the box function using method chaining.<\/p>\n\n\n\n<p>JavaScript, along with jQuery, offers powerful tools to enhance web applications efficiently. By leveraging built-in functions, extending prototypes, and using $.fn, developers can create more dynamic and interactive user experiences. Whether you&#8217;re working on a simple project or a complex dynamic web app, mastering these techniques will help streamline your development process. I hope this article has provided valuable insights into utilizing JavaScript effectively. Happy coding!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hi, good day to our valued blog readers! My name is Htay Min Khaung, and I work at WIT Lab Co., Ltd. Today, I would like to share how to use some useful JavaScript functions in web app projects. JavaScript is one of the most powerful languages in the web development environment. JQuery is also [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":605,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-603","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-system"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v23.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Useful Functions of JavaScript And JQuery - WIT LAB %<\/title>\n<meta name=\"description\" content=\"We excel in utilizing cutting-edge technology, programming languages, and frameworks to deliver high-quality digital solutions.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/witlab.ph\/blog\/useful-functions-of-javascript-and-jquery\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Useful Functions of JavaScript And JQuery - WIT LAB %\" \/>\n<meta property=\"og:description\" content=\"We excel in utilizing cutting-edge technology, programming languages, and frameworks to deliver high-quality digital solutions.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/witlab.ph\/blog\/useful-functions-of-javascript-and-jquery\/\" \/>\n<meta property=\"og:site_name\" content=\"WIT LAB\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/people\/WIT-LAB\/61567795364273\/\" \/>\n<meta property=\"article:published_time\" content=\"2025-02-14T08:14:49+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-02-14T08:14:50+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/witlab.ph\/blog\/wp-content\/uploads\/2025\/02\/cover.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"700\" \/>\n\t<meta property=\"og:image:height\" content=\"366\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Htay Min Kaung\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Htay Min Kaung\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":[\"Article\",\"BlogPosting\"],\"@id\":\"https:\/\/witlab.ph\/blog\/useful-functions-of-javascript-and-jquery\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/witlab.ph\/blog\/useful-functions-of-javascript-and-jquery\/\"},\"author\":{\"name\":\"Htay Min Kaung\",\"@id\":\"https:\/\/witlab.ph\/blog\/#\/schema\/person\/c37bd963f520e565eebe682a9e319680\"},\"headline\":\"Useful Functions of JavaScript And JQuery\",\"datePublished\":\"2025-02-14T08:14:49+00:00\",\"dateModified\":\"2025-02-14T08:14:50+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/witlab.ph\/blog\/useful-functions-of-javascript-and-jquery\/\"},\"wordCount\":423,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/witlab.ph\/blog\/#organization\"},\"image\":{\"@id\":\"https:\/\/witlab.ph\/blog\/useful-functions-of-javascript-and-jquery\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/witlab.ph\/blog\/wp-content\/uploads\/2025\/02\/cover.jpg\",\"articleSection\":[\"System\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/witlab.ph\/blog\/useful-functions-of-javascript-and-jquery\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/witlab.ph\/blog\/useful-functions-of-javascript-and-jquery\/\",\"url\":\"https:\/\/witlab.ph\/blog\/useful-functions-of-javascript-and-jquery\/\",\"name\":\"Useful Functions of JavaScript And JQuery - WIT LAB %\",\"isPartOf\":{\"@id\":\"https:\/\/witlab.ph\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/witlab.ph\/blog\/useful-functions-of-javascript-and-jquery\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/witlab.ph\/blog\/useful-functions-of-javascript-and-jquery\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/witlab.ph\/blog\/wp-content\/uploads\/2025\/02\/cover.jpg\",\"datePublished\":\"2025-02-14T08:14:49+00:00\",\"dateModified\":\"2025-02-14T08:14:50+00:00\",\"description\":\"We excel in utilizing cutting-edge technology, programming languages, and frameworks to deliver high-quality digital solutions.\",\"breadcrumb\":{\"@id\":\"https:\/\/witlab.ph\/blog\/useful-functions-of-javascript-and-jquery\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/witlab.ph\/blog\/useful-functions-of-javascript-and-jquery\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/witlab.ph\/blog\/useful-functions-of-javascript-and-jquery\/#primaryimage\",\"url\":\"https:\/\/witlab.ph\/blog\/wp-content\/uploads\/2025\/02\/cover.jpg\",\"contentUrl\":\"https:\/\/witlab.ph\/blog\/wp-content\/uploads\/2025\/02\/cover.jpg\",\"width\":700,\"height\":366},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/witlab.ph\/blog\/useful-functions-of-javascript-and-jquery\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/witlab.ph\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Useful Functions of JavaScript And JQuery\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/witlab.ph\/blog\/#website\",\"url\":\"https:\/\/witlab.ph\/blog\/\",\"name\":\"WIT LAB\",\"description\":\"Web Development\",\"publisher\":{\"@id\":\"https:\/\/witlab.ph\/blog\/#organization\"},\"alternateName\":\"WIT LAB INC\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/witlab.ph\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/witlab.ph\/blog\/#organization\",\"name\":\"WIT LAB INC\",\"alternateName\":\"Spiceworks (Japan)\",\"url\":\"https:\/\/witlab.ph\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/witlab.ph\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/witlab.ph\/blog\/wp-content\/uploads\/2024\/09\/logo_witlab-Copy-Copy.png\",\"contentUrl\":\"https:\/\/witlab.ph\/blog\/wp-content\/uploads\/2024\/09\/logo_witlab-Copy-Copy.png\",\"width\":681,\"height\":616,\"caption\":\"WIT LAB INC\"},\"image\":{\"@id\":\"https:\/\/witlab.ph\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/people\/WIT-LAB\/61567795364273\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/witlab.ph\/blog\/#\/schema\/person\/c37bd963f520e565eebe682a9e319680\",\"name\":\"Htay Min Kaung\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/witlab.ph\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/0a90aff2f83f2079748817650f0848ff?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/0a90aff2f83f2079748817650f0848ff?s=96&d=mm&r=g\",\"caption\":\"Htay Min Kaung\"},\"url\":\"https:\/\/witlab.ph\/blog\/author\/htay-min-kaung\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Useful Functions of JavaScript And JQuery - WIT LAB %","description":"We excel in utilizing cutting-edge technology, programming languages, and frameworks to deliver high-quality digital solutions.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/witlab.ph\/blog\/useful-functions-of-javascript-and-jquery\/","og_locale":"en_US","og_type":"article","og_title":"Useful Functions of JavaScript And JQuery - WIT LAB %","og_description":"We excel in utilizing cutting-edge technology, programming languages, and frameworks to deliver high-quality digital solutions.","og_url":"https:\/\/witlab.ph\/blog\/useful-functions-of-javascript-and-jquery\/","og_site_name":"WIT LAB","article_publisher":"https:\/\/www.facebook.com\/people\/WIT-LAB\/61567795364273\/","article_published_time":"2025-02-14T08:14:49+00:00","article_modified_time":"2025-02-14T08:14:50+00:00","og_image":[{"width":700,"height":366,"url":"https:\/\/witlab.ph\/blog\/wp-content\/uploads\/2025\/02\/cover.jpg","type":"image\/jpeg"}],"author":"Htay Min Kaung","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Htay Min Kaung","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["Article","BlogPosting"],"@id":"https:\/\/witlab.ph\/blog\/useful-functions-of-javascript-and-jquery\/#article","isPartOf":{"@id":"https:\/\/witlab.ph\/blog\/useful-functions-of-javascript-and-jquery\/"},"author":{"name":"Htay Min Kaung","@id":"https:\/\/witlab.ph\/blog\/#\/schema\/person\/c37bd963f520e565eebe682a9e319680"},"headline":"Useful Functions of JavaScript And JQuery","datePublished":"2025-02-14T08:14:49+00:00","dateModified":"2025-02-14T08:14:50+00:00","mainEntityOfPage":{"@id":"https:\/\/witlab.ph\/blog\/useful-functions-of-javascript-and-jquery\/"},"wordCount":423,"commentCount":0,"publisher":{"@id":"https:\/\/witlab.ph\/blog\/#organization"},"image":{"@id":"https:\/\/witlab.ph\/blog\/useful-functions-of-javascript-and-jquery\/#primaryimage"},"thumbnailUrl":"https:\/\/witlab.ph\/blog\/wp-content\/uploads\/2025\/02\/cover.jpg","articleSection":["System"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/witlab.ph\/blog\/useful-functions-of-javascript-and-jquery\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/witlab.ph\/blog\/useful-functions-of-javascript-and-jquery\/","url":"https:\/\/witlab.ph\/blog\/useful-functions-of-javascript-and-jquery\/","name":"Useful Functions of JavaScript And JQuery - WIT LAB %","isPartOf":{"@id":"https:\/\/witlab.ph\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/witlab.ph\/blog\/useful-functions-of-javascript-and-jquery\/#primaryimage"},"image":{"@id":"https:\/\/witlab.ph\/blog\/useful-functions-of-javascript-and-jquery\/#primaryimage"},"thumbnailUrl":"https:\/\/witlab.ph\/blog\/wp-content\/uploads\/2025\/02\/cover.jpg","datePublished":"2025-02-14T08:14:49+00:00","dateModified":"2025-02-14T08:14:50+00:00","description":"We excel in utilizing cutting-edge technology, programming languages, and frameworks to deliver high-quality digital solutions.","breadcrumb":{"@id":"https:\/\/witlab.ph\/blog\/useful-functions-of-javascript-and-jquery\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/witlab.ph\/blog\/useful-functions-of-javascript-and-jquery\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/witlab.ph\/blog\/useful-functions-of-javascript-and-jquery\/#primaryimage","url":"https:\/\/witlab.ph\/blog\/wp-content\/uploads\/2025\/02\/cover.jpg","contentUrl":"https:\/\/witlab.ph\/blog\/wp-content\/uploads\/2025\/02\/cover.jpg","width":700,"height":366},{"@type":"BreadcrumbList","@id":"https:\/\/witlab.ph\/blog\/useful-functions-of-javascript-and-jquery\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/witlab.ph\/blog\/"},{"@type":"ListItem","position":2,"name":"Useful Functions of JavaScript And JQuery"}]},{"@type":"WebSite","@id":"https:\/\/witlab.ph\/blog\/#website","url":"https:\/\/witlab.ph\/blog\/","name":"WIT LAB","description":"Web Development","publisher":{"@id":"https:\/\/witlab.ph\/blog\/#organization"},"alternateName":"WIT LAB INC","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/witlab.ph\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/witlab.ph\/blog\/#organization","name":"WIT LAB INC","alternateName":"Spiceworks (Japan)","url":"https:\/\/witlab.ph\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/witlab.ph\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/witlab.ph\/blog\/wp-content\/uploads\/2024\/09\/logo_witlab-Copy-Copy.png","contentUrl":"https:\/\/witlab.ph\/blog\/wp-content\/uploads\/2024\/09\/logo_witlab-Copy-Copy.png","width":681,"height":616,"caption":"WIT LAB INC"},"image":{"@id":"https:\/\/witlab.ph\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/people\/WIT-LAB\/61567795364273\/"]},{"@type":"Person","@id":"https:\/\/witlab.ph\/blog\/#\/schema\/person\/c37bd963f520e565eebe682a9e319680","name":"Htay Min Kaung","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/witlab.ph\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/0a90aff2f83f2079748817650f0848ff?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/0a90aff2f83f2079748817650f0848ff?s=96&d=mm&r=g","caption":"Htay Min Kaung"},"url":"https:\/\/witlab.ph\/blog\/author\/htay-min-kaung\/"}]}},"_links":{"self":[{"href":"https:\/\/witlab.ph\/blog\/wp-json\/wp\/v2\/posts\/603"}],"collection":[{"href":"https:\/\/witlab.ph\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/witlab.ph\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/witlab.ph\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/witlab.ph\/blog\/wp-json\/wp\/v2\/comments?post=603"}],"version-history":[{"count":12,"href":"https:\/\/witlab.ph\/blog\/wp-json\/wp\/v2\/posts\/603\/revisions"}],"predecessor-version":[{"id":619,"href":"https:\/\/witlab.ph\/blog\/wp-json\/wp\/v2\/posts\/603\/revisions\/619"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/witlab.ph\/blog\/wp-json\/wp\/v2\/media\/605"}],"wp:attachment":[{"href":"https:\/\/witlab.ph\/blog\/wp-json\/wp\/v2\/media?parent=603"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/witlab.ph\/blog\/wp-json\/wp\/v2\/categories?post=603"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/witlab.ph\/blog\/wp-json\/wp\/v2\/tags?post=603"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}