Advertisement
Guest User

events and elements

a guest
Apr 22nd, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. index.html
  2. --------------------------------
  3. <!doctype html>
  4. <html>
  5.  
  6.     <head>
  7.    
  8.         <script src="javascript.js"></script>
  9.                    
  10.    
  11.     </head>
  12.    
  13.     <body bgColor="mediumaquamarine">
  14.         <ul id="myul" oncontextmenu="return false;">
  15.             <li>the list item</li>
  16.             <li>the second item</li>
  17.         </ul>
  18.        
  19.         <input type="button" onclick="buttonClicked()" value="click me"/>
  20.        
  21.         <div class="divclass" id="mydiv" onmouseover="mouseOverDiv()" onmouseout="mouseOutOfDiv()"></div>
  22.        
  23.         <select id="mySelect" onchange="colorSelected(this.value)">
  24.             <option value="" selected="true">----</option>
  25.             <option value="#0094ff">Light Blue</option>
  26.             <option value="#f78c09">Dark Orange</option>
  27.             <option value="#b200ff">Purple</option>
  28.         </select>
  29.         <br/>
  30.         <select id="lstcountries" onchange="SetSelectedValue()">
  31.            <option>-- Select Country --</option>
  32.            <option value="US">United State</option>
  33.            <option value="UK">United Kingdom</option>
  34.            <option value="IL">Israel</option>
  35.         </select>
  36.         <input type="text" id="txtCountry" disabled="disabled" />
  37.        
  38.         <br/>
  39.        
  40.          <input type="radio" name="rColors" value="Red" /> Red <br />
  41.          <input type="radio" name="rColors" value="Yellow" /> Yellow <br />
  42.          <input type="radio" name="rColors" value="Green" /> Green <br />
  43.  
  44.          <input type="button" value="ShowColor" onclick="ShowSelectedColor()" />
  45.  
  46.  
  47.        
  48.     </body>
  49.  
  50. </html>
  51. --------------------------------
  52.  
  53.  
  54. javascript.js
  55. ----------------------------------
  56.  
  57. document.onload = loaded();
  58. var initialBg;
  59. var hasChanged = false;
  60.  
  61. function loaded(){
  62.     console.log("document has been loaded");
  63. }
  64.  
  65.  
  66. function buttonClicked(){
  67.     console.log("clicked");
  68.     var ulElem = document.getElementById("myul");
  69.     console.log(ulElem);
  70.     ulElem.style = "background-color:red; list-style-type: none;";
  71.    
  72.     var liElems = document.getElementsByTagName("li");
  73.     console.log(liElems);
  74.    
  75.     for(var i=0; i<liElems.length; i+=1){
  76.         liElems[i].style = "padding-left: 100px";
  77.     }
  78.    
  79.    
  80.     var divElem = document.getElementsByClassName("divclass");
  81.     console.log(divElem);
  82.     divElem = divElem[0];// reassigning divElem to the first element in the collection
  83.     console.log(divElem);
  84.    
  85.     //divElem.innerText="<h1>My Dynamic H1</h1> <br/> <h1>My Dynamic H1 2</h1>"; // innerText does NOT parse the content
  86.     divElem.innerHTML="<h1>My Dynamic H1</h1> <br/> <h1>My Dynamic H1 2</h1>"; // innerText does  parse the content
  87.  
  88. }
  89.  
  90. function mouseOverDiv(){
  91.    
  92.     var divElem = document.getElementById("mydiv");
  93.     divElem.style.backgroundColor = "blue";
  94.    
  95. }
  96.  
  97. function mouseOutOfDiv(){
  98.     var divElem = document.getElementById("mydiv");
  99.     divElem.style.backgroundColor = "";
  100. }
  101.  
  102. function colorSelected(color){
  103.     if(hasChanged==false){
  104.         initialBg = document.body.bgColor;
  105.         hasChanged = true;
  106.     }
  107.     if(color == ""){
  108.         color = initialBg;
  109.     }
  110.     document.body.bgColor = color;
  111. }
  112.  
  113.  function SetSelectedValue() {
  114.        var countries = document.getElementById("lstcountries");
  115.        for (var i = 0; i < countries.options.length; i++) {
  116.         if (countries.options[i].selected){
  117.             document.getElementById("txtCountry").value = countries.options[i].value;
  118.             break;
  119.         }
  120.      }
  121.  }
  122.  
  123.   function ShowSelectedColor(){
  124.   var rb = document.getElementsByName("rColors");
  125.   var val="";
  126.   for (var i = 0; i < rb.length; i++){  
  127.       if (rb[i].checked){
  128.          val = rb[i].value;
  129.          break;
  130.       }
  131.  
  132.     }
  133.     if(val != ""){
  134.         alert("Selected color is " + val);
  135.     }
  136. }
  137.  
  138. ----------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement