Vložení stringu z JS do HTML

Webdesign, HTML, CSS, Flash, PHP, ASP, .NET, JavaScript. Kritika www stránek na Smetišti.

Moderátor: Moderátoři Živě.cz

Odeslat příspěvekod navrc111 8. 2. 2023 10:56

Dobrý den už se tímhle trápím dlouho teprve začínám tak děkuji za jakoukoli radu.

String se mě zobrazuje pouze v <input id="string" type="text" /></p> ale já ho potřebuji vložit do běžného textu ne do input.
Tohleto prostě nefunguje: <p id="string">text</p>

Zde je celí kód:
Kód: Vybrat vše
<!DOCTYPE html>
<html lang="cs">

    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Info_Panel v 0.1</title>

        <style>

            body
            {
                color: rgb(157, 255, 0);
                background: black;
                font-family: Arial, Helvetica, sans-serif;
                text-align: center;
            }

            footer
            {
                color: rgb(48, 73, 67);
                margin: 0;
                font-size: 12px;
            }
     
            article
            {
                border: 1px solid #ccc;

                margin: 50px;
                margin-left: auto;
                margin-right: auto;
                padding: 50px;

                max-width: 420px;
            }
        </style>
       
        <script type="text/javascript" src="jquery-1.12.1.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function ()
            {
                GetData();
            });

            function GetData()
            {
                var url = 'https://api.thingspeak.com/channels/1981429/feeds.json?key=PRJA98LHWOBMFOZV&results=1';
                $.ajax
                ({
                    url: url,
                    type: 'GET',
                    contentType: "application/json",
                    //dataType: "json",
                    //crossDomain: true,
                    success: function (data, textStatus, xhr) {
                        $.each(data, function (i, item) {
                            if (i == 'feeds') {
                                var ubound = item.length;
                                $('#txtField1').val(item[ubound - 1].field1);
                                $('#txtField2').val(item[ubound - 1].field2);
                                $('#txtField3').val(item[ubound - 1].field3);
                            }
                        });
                    },
                    error: function (xhr, textStatus, errorThrown) {
                        alert(errorThrown);
                    }
                });

                setTimeout(GetData, 10000);
            }
        </script>
    </head>

    <body>
        <article>
            <header>
                <h1>Info_Panel</h1>
           </header>
           
            <p>Mamča: <input id="txtField2" type="text" /></p>
            <p>Tech: <input id="txtField3" type="text" /></p>

           
            <p id="txtField1">test</p>


            <footer>
               
            </footer>

        </article>
    </body>
</html>

Děkuji všem za radu už si nevím rady.
navrc111
Kolemjdoucí

Odeslat příspěvekod Wikan 8. 2. 2023 11:02

Místo funkce val() použij pro ten neinputový element funkci text() případně html().
Wikan
Moderátor
Uživatelský avatar

Odeslat příspěvekod navrc111 8. 2. 2023 11:11

Děkuji mnohokrát. 8-D
navrc111
Kolemjdoucí

Odeslat příspěvekod Wikan 8. 2. 2023 11:23

A možná bych zvážil možnost nepoužívat jQuery a to ještě v takhle staré verzi. Přece jenom moderní JS poskytuje mnohem víc možností než v době kdy jQuery vzniklo a mělo své opodstatnění.
Kód: Vybrat vše
<script type="text/javascript">
    getData();

    async function getData() {
      const url =
        "https://api.thingspeak.com/channels/1981429/feeds.json?key=PRJA98LHWOBMFOZV&results=1";

      const response = await fetch(url);
      const json = await response.json();
      const item = json?.feeds[json.feeds.length - 1];
      if (item) {
        document.getElementById("txtField1").textContent = item.field1;
        document.getElementById("txtField2").value = item.field2;
        document.getElementById("txtField3").value = item.field3;
      }
      setTimeout(getData, 10000);
    }
</script>
Wikan
Moderátor
Uživatelský avatar

Odeslat příspěvekod navrc111 8. 2. 2023 12:34

Týjo dík je to i mnohem srozumitelnější. Super typ
navrc111
Kolemjdoucí


Kdo je online

Uživatelé procházející toto fórum: Žádní registrovaní uživatelé a 0 návštevníků