Advertisement
Sourabh_5050

WTL lab4

Sep 5th, 2023
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5.36 KB | Source Code | 0 0
  1. lab4a) color changing
  2.  
  3. <html>
  4. <head>
  5.     <title>Assignment 4 A</title>
  6.     <script>
  7.         var i=0;
  8.         function Callme()
  9.         {
  10.          var color=['red','green','teal','yellow','silver','grey','purple','lime','black','blue','aqua','white'];
  11.          document.bgColor=color[i];
  12.          i++;
  13.          if(i==color.length)
  14.          i=0;
  15.         }
  16.     </script>
  17. </head>
  18. <body>
  19. <p>Click Button to Change color </p>
  20. <input type="button" value="change color" onclick="Callme()"
  21. </body>
  22.  
  23. <!---<body>
  24.    <img src=" " id="im" height="400" width="400">
  25.    <br><hr><br>
  26.    <input type="button" value="click" onclick="fun1()">
  27.  
  28.    <script>
  29.        var i=0;
  30.        function fun1(){
  31.  
  32.            var images=['accessory1.jpg','accessory2.jpg','accessory3.jpg','cafe.jpg.jpg']
  33.            x=document.getElementById('im');
  34.            x.src=images[i];
  35.            i++;
  36.            if(i==images.length)
  37.  
  38.            {
  39.                i=0;
  40.            }
  41.        }
  42.    </script>
  43. </body>---->
  44.  
  45. </body>
  46. </html>
  47.  
  48. 4b) check for color is present or not
  49. <html>
  50.     <head>
  51.  
  52.     </head>
  53.     <body>
  54.         Enter a color:<input type="text" id="text1">
  55.         <input type="button" value="Click" onclick="func1()">
  56.         <div id="d1">New Color:</div>
  57.         <script>
  58.             var color=['red','green','yellow'];
  59.             function func1()
  60.             {
  61.                 var text1=document.getElementById('text1').value;
  62.                 var newcolor=text1.toLowerCase();
  63.                 if(color.indexOf(newcolor)==-1)
  64.                 {
  65.                     alert("Color not present in the list ");
  66.                     color.push(newcolor);
  67.                     alert(color);
  68.                 }
  69.                 else
  70.                 {
  71.                     alert(newcolor+" is already present");
  72.                 }
  73.                 document.getElementById("d1").innerHTML=color;
  74.             }
  75.         </script>
  76.  
  77.     </body>
  78. </html>
  79.  
  80. 4c) background with choose option
  81. <!DOCTYPE html>
  82. <html lang="en">
  83. <head>
  84.     <meta charset="UTF-8">
  85.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  86.     <title>Font and Background Color Changer</title>
  87. </head>
  88. <body>
  89.     <h1 id="heading">Sample Heading</h1>
  90.     <label for="fontColor">Select Font Color:</label>
  91.     <select id="fontColor">
  92.         <option value="black">Black</option>
  93.         <option value="red">Red</option>
  94.         <option value="blue">Blue</option>
  95.         <option value="green">Green</option>
  96.     </select>
  97.  
  98.     <label for="bgColor">Select Background Color:</label>
  99.     <select id="bgColor">
  100.         <option value="white">White</option>
  101.         <option value="yellow">Yellow</option>
  102.         <option value="blue">Blue</option>
  103.         <option value="green">Green</option>
  104.     </select>
  105.  
  106.     <script>
  107.         var heading = document.getElementById("heading");
  108.         var fontColorDropdown = document.getElementById("fontColor");
  109.         var bgColorDropdown = document.getElementById("bgColor");
  110.  
  111.         fontColorDropdown.addEventListener("change", applyStyles);
  112.         bgColorDropdown.addEventListener("change", applyStyles);
  113.  
  114.         function applyStyles() {
  115.             var selectedFontColor = fontColorDropdown.value;
  116.             var selectedBgColor = bgColorDropdown.value;
  117.  
  118.             // Get the current font color and background color of the heading
  119.             var currentFontColor = getComputedStyle(heading).color;
  120.             var currentBgColor = getComputedStyle(heading).backgroundColor;
  121.  
  122.             // Check if the selected colors are different from the current styles
  123.             if (selectedFontColor !== currentFontColor || selectedBgColor !== currentBgColor) {
  124.                 heading.style.color = selectedFontColor;
  125.                 heading.style.backgroundColor = selectedBgColor;
  126.             }
  127.         }
  128.     </script>
  129. </body>
  130. </html>
  131.  
  132. 4d) check array is present in the list or not
  133. <!DOCTYPE html>
  134. <html lang="en">
  135. <head>
  136.     <meta charset="UTF-8">
  137.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  138.     <title>Character Occurrences in Array</title>
  139. </head>
  140. <body>
  141.     <h1>Character Occurrences in Array</h1>
  142.    
  143.     <!-- Input for entering the character to count -->
  144.     <label for="charToCount">Enter a character:</label>
  145.     <input type="text" id="charToCount">
  146.     <button onclick="countOccurrences()">Count</button>
  147.  
  148.     <!-- Display area for the result -->
  149.     <p id="result"></p>
  150.  
  151.     <script>
  152.         function countOccurrences() {
  153.             // Get the character to count from the input field
  154.             var charToCount = document.getElementById("charToCount").value;
  155.  
  156.             // Declare a JavaScript string array
  157.             var arr = ["a", "b", "c", "d", "a", "c", "g", "h","i","j","k","k"];
  158.  
  159.             // Initialize a counter for the character occurrences
  160.             var charCount = 0;
  161.  
  162.             // Loop through the array to count occurrences
  163.             for (var i = 0; i < arr.length; i++) {
  164.                if (arr[i] === charToCount) {
  165.                    charCount++;
  166.                }
  167.            }
  168.  
  169.            // Display the occurrences of the specified character
  170.            var resultElement = document.getElementById("result");
  171.            resultElement.textContent = "Occurrences of " + charToCount + " is " + charCount;
  172.        }
  173.    </script>
  174. </body>
  175. </html>
  176.  
  177.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement