Editing JavaScript 1 Document Objekt

Jump to navigation Jump to search

Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then publish the changes below to finish undoing the edit.

Latest revision Your text
Line 208: Line 208:
   </form>
   </form>
   <script>
   <script>
    const selectionList = document.getElementById('selection-list');
     // Möglichkeit 1 um entsprechendes Optionsfeld auszuwählen     
     // Möglichkeit 1 um entsprechendes Optionsfeld auszuwählen     
     selectionList.selectedIndex = 2;     
     selection-list.selectedIndex = 2;     
     // Möglichkeit 2     
     // Möglichkeit 2     
     selectionList.options[2].selected = true;   
     selection-list.options[2].selected = true;   
     // Möglichkeit 3     
     // Möglichkeit 3     
     selectionList.value = "selection3";
     selection-list.value = "selection3";
   </script>
   </script>
</body>
</body>
Line 250: Line 249:
     * Wenn Fokus entfernt wird, kommt es zum blur-Event     
     * Wenn Fokus entfernt wird, kommt es zum blur-Event     
     */
     */
     document.getElementById("field1").onfocus = message;
     field1.onfocus = message;
   </script>
   </script>
</body>
</body>
Line 279: Line 278:
     * tritt die Fehlermeldung auf     
     * tritt die Fehlermeldung auf     
     */
     */
     document.getElementById("field1").onblur = message;
     field1.onblur = message;
   </script>
   </script>
</body>
</body>
Line 352: Line 351:
<pre>
<pre>
<body>
<body>
   <form id="form" action="evalData.html" method="GET" onsubmit="return eval()">
   <form id="formular" action="fertig.html" method="GET" onsubmit="return auswerten()">
     <div class="form-row">  
     <div class="form-row">  
       <label for="inputname">Name</label>  
       <label for="inputname">Name</label>  
Line 362: Line 361:
     </div>
     </div>
     <div class="form-row">  
     <div class="form-row">  
       <label for="inputage">Alter</label>  
       <label for="inputalter">Alter</label>  
       <input id="inputage" name="age">  
       <input id="inputalter" name="alter">  
     </div>  
     </div>  
     <button type="submit">Absenden</button>
     <button type="submit">Absenden</button>
   </form>
   </form>
   <script>
   <script>
     function eval() {
     function auswerten() {
       if (inputname.value == "") {
       if (inputname.value == "") {
         alert("Gib einen Namen ein");
         alert("Gib einen Namen ein");
Line 379: Line 378:
         return false;
         return false;
       }
       }
       if (inputage.value == "") {
       if (inputalter.value == "") {
         alert("Gib ein Alter ein");
         alert("Gib ein Alter ein");
         inputage.focus();
         inputalter.focus();
         return false;
         return false;
       }
       }
       let number = true;
       let zahl = true;
       console.log(inputage.value.length);
       console.log(inputalter.value.length);
       for (let i = 0; i < inputage.value.length; ++i) {
       for (let i = 0; i < inputalter.value.length; ++i) {
         if (inputage.value.charAt(i) < "0" ||
         if (inputalter.value.charAt(i) < "0" ||
           inputage.value.charAt(i) > "9") {
           inputalter.value.charAt(i) > "9") {
           number= false;
           zahl = false;
         }
         }
       }
       }
       if (!zahl) {
       if (!zahl) {
         alert("Gib eine Zahl ein");
         alert("Gib eine Zahl ein");
         inputage.focus();
         inputalter.focus();
         return false;
         return false;
       }
       }
Line 423: Line 422:
   <button type="button" id="btn">Zur neuen Seite</button>
   <button type="button" id="btn">Zur neuen Seite</button>
   <script>
   <script>
     function loadPage() {  
     function laden() {  
       // Adresse der Seite ist unter location.href verfügbar         
       // Adresse der Seite ist unter location.href verfügbar         
       // hier wird der Besucher gleich auf die Seite         
       // hier wird der Besucher gleich auf die Seite         
Line 431: Line 430:
     }     
     }     
     document.getElementById("absatz").innerHTML = location.href;     
     document.getElementById("absatz").innerHTML = location.href;     
     btn.onmouseover = loadPage;
     btn.onmouseover = laden;
   </script>
   </script>
</body>
</body>
Line 440: Line 439:
<body>
<body>
   <form method="GET" action="js.html">  
   <form method="GET" action="js.html">  
     <input type="text" name="question" placeholder="frag was">  
     <input type="text" name="frage" placeholder="frag was">  
     <button type="submit">Abschicken</button>
     <button type="submit">Abschicken</button>
   </form>
   </form>
   <p id="paragraph"></p>
   <p id="absatz"></p>
   <script>
   <script>
     "use strict";  
     "use strict";  
     // für Formulare die mit GET-Methode abeschickt werden     
     // für Formulare die mit GET-Methode abeschickt werden     
     // ruft mit search-Attribut Teil der URL  
     // ruft mit search-Attribut Teil der URL  
     // mit Formularinhalten ab
     // mit Formularinhalten ab     
    
     let str = location.search;     
     let str = location.search;     
     // dann wird mit substr() das Fragezeichen entfernt  
     // dann wird mit substr() das Fragezeichen entfernt  
Line 455: Line 453:
     // das Fragezeichen steht immer an erster  
     // das Fragezeichen steht immer an erster  
     // Stelle deshalb substr(1)  
     // Stelle deshalb substr(1)  
     str = str.substr(1);  
     str = str.substr(1);  
     // In der URL sind einzelne Suchwörter durch das &-
     // In der URL sind einzelne Suchwörter durch das &-
     // Zeichen miteinander verbunden
     // Zeichen miteinander verbunden
     // diese voneinander trennen => split()-Methode  
     // diese voneinander trennen => split()-Methode  
     let arr = str.split('&');
     let arr = str.split('&');
     for (let i = 0; i < arr.length; i++) {
     for (let i = 0; i < arr.length; i++) {
       arr[i] = arr[i].split('=');
       arr[i] = arr[i].split('=');
     }
     }
     let content = "";
     let inhalt = "";
     for (let value of arr) {
     for (let wert of arr) {
       content += value[0] + ": " + value[1] + "<br>";
       inhalt += wert[0] + ": " + wert[1] + "<br>";
     }
     }  
     document.getElementById("paragraph").innerHTML = content;
    // decodeURIComponent wird benötigt um
    // zb das @ Zeichen richtig darzustellen   
     document.getElementById("absatz").innerHTML = decodeURIComponent(inhalt);
   </script>
   </script>
</body>
</body>
Line 488: Line 486:
     "use strict";
     "use strict";


     function changeImage() {
     function tauschen() {
       document.images[0].src = "bild2.jpg";
       document.images[0].src = "bild2.jpg";
     }
     }
     btn.onclick = changeImage;
     btn.onclick = tauschen;
   </script>
   </script>
</body>
</body>
Line 502: Line 500:
<body>
<body>
   <div id="div">
   <div id="div">
     <p id="paragraph">Hier steht ein Absatz</p>
     <p id="absatz">Hier steht ein Absatz</p>
   </div>
   </div>
   <button type="button" onclick="background()">Layout verändern</button>
   <button type="button" onclick="hintergrund()">Layout verändern</button>
   <script>
   <script>
     function background() {
     function hintergrund() {
       document.getElementById("paragraph").style.background = "red";
       document.getElementById("absatz").style.background = "red";
       document.getElementById("paragraph").style.fontSize = "30px";
       document.getElementById("absatz").style.fontSize = "30px";
       document.getElementById("paragraph").style.color = "white";
       document.getElementById("absatz").style.color = "white";
       document.getElementById("paragraph").style.width = "150px";
       document.getElementById("absatz").style.width = "150px";
       document.getElementById("paragraph").style.border = "3px solid blue";
       document.getElementById("absatz").style.border = "3px solid blue";
     }
     }
   </script>
   </script>
</body>
</body>
</pre>
</pre>
<blockquote>
Quelle: JavaScript
Programmieren für Einsteiger
ISBN: 978-3-96645-016-4
</blockquote>

Please note that all contributions to Coders.Bay Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see CB Wiki:Copyrights for details). Do not submit copyrighted work without permission!

Cancel Editing help (opens in new window)