jQuery CSS: How to Change Values in the Tag</strong></p> <p><strong>Question:</strong></p> <p>It is known that jQuery can be used to apply styles to HTML elements. However, in certain scenarios, users might want to modify the values within the <style> tag itself, rather than just affecting a specific element. Is this achievable with jQuery?</p> <p><strong>Example:</strong></p> <p>Consider a situation where the background color of the body element needs to be changed by altering the <style> tag:</p> <div class="code" style="position:relative; padding:0px; margin:0px;"><pre><style title="css_style" type="text/css"> body { background-color:#dc2e2e; /* <- CHANGE THIS */ color:#000000; font-family:Tahoma, Verdana; font-size:11px; margin:0px; padding:0px; background-image: url(http://abc.de/image.jpg); } ... // .... Copy after login jQuery Code: jQuery can modify specific styles for an HTML element: $('body').css('background-color','#ff0000');Copy after login However, this approach adds styles inline to the body tag instead of the tag.</p> <p><strong>Solution:</strong></p> <p>To change the value in the <style> tag using jQuery, a different approach is required. Instead of modifying existing style elements, jQuery can be used to create a new style element:</p> <div class="code" style="position:relative; padding:0px; margin:0px;"><pre>$( "<style>body { background: black; }" ).appendTo( "head" )Copy after login By cascading, this newly created style element overrides existing styles, effectively changing the value in the tag.</p>