This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm whether you accept or reject these cookies being set.

A cookie will be stored in your browser regardless of choice to prevent you being asked this question again. You will be able to change your cookie settings at any time using the link in the footer.

Themabewertung:
  • 0 Bewertung(en) - 0 im Durchschnitt
  • 1
  • 2
  • 3
  • 4
  • 5
Variable in Input <type='text'> ausgeben
#1
Hallo,
Ich versuche in meinen Anfängen in JavaScript eine let-Variable in einem input type='text' zu übertragen. Im Internet ich habe bereits Beispielcode gefunden, leider habe ich diese nicht zum laufen gebracht... In die Überschrift habe ich meine Variable geschrieben bekommen...
<!DOCTYPE html> <!-- For more information visit: https://fipsok.de -->



<html lang="de">
   <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <link rel="stylesheet" href="Vorlage_fur_style.css">
      <title>Titel Registerfenster</title>
      <script type="text/javascript">
         let Pflanze = "Boskop";
         document.getElementById("id_Pflanzen_Name").value = Pflanze;
      </script>
   </head>
   <body>
      <form name='fo' method='Post' action='./'>
         <table class='Tabelle'>
            <tr>
               <td colspan='2' class='Zelle_Uberschrift'><script type="text/javascript">document.write(Pflanze)</script></td>
            </tr>
            <tr class='Zeile'>
               <td class='Zelle'>Pflanze-Name<input class='Textfeld_Name' type='text' name='Na_Pflanzen_Name' id='id_Pflanzen_Name' /></td>
            <tr>
         </table>
      </form>
   </body>
</html>

Gruß
Zitieren
#2
Hi Faultier_1982,
der Ansatz ist schon mal richtig aber da fehlt noch was. Du solltest kein document.write verwenden. Hier kannst du lieber das entsprechende Element in JavaScript selektieren und dann den Inhalt via innerText,  innerHTML oder textContent. Bevor du aber auf Elemente zugreifst solltest du überprüfen ob der DOM geladen wurde - sprich der HTML-Code. Das kannst du mithilfe eines EventListener und einem Event namens 'DOMContentLoaded'.

Hier eine folgende Lösung:
'use strict';
document.addEventListener('DOMContentLoaded', () => {
let pflanze = 'Kaktus';
document.getElementById('id_Pflanzen_Name').value = pflanze;
});

Viel Erfolg!

VG rzscout
Als Lösung markieren Zitieren


Gehe zu:


Benutzer, die gerade dieses Thema anschauen: 1 Gast/Gäste