简体   繁体   中英

Javascript: input and button. Function doesn't work -> Document.getElementById is not a function

Simple Code:

    <div id="right">
        <h2>Zamiana jednostek temperatury</h2>
        temperatura w <sup>o</sup>Celsjusza<br>

        <input type="text" id="cyfry" name="cyfry"><br>
        <button onclick="fahrenheit()">Fahrenheit</button>
        <button onclick="kelwin()">Kelwin</button>
        <span id="wynik"></span>
    </div>

And Script:

function fahrenheit(){
    var x=Document.getElementById("cyfry");
    parseInt(x);
    x = (x*1,8)+32;
    document.getElementById("wynik").innerHTML=x;
}
function kelwin(){
    var x=Document.getElementById("cyfry");
    parseInt(x);
    x = x + 273,15;
    document.getElementById("wynik").innerHTML=x;
}

As you can see all I want to do is convert units in Celsius to Fahrenheit or Calvins, but I get an error:

Uncaught TypeError: Document.getElementById is not a function
    at fahrenheit (script.js:2)
    at HTMLButtonElement.onclick (index.html?cyfry=:32)

Thanks a lot in advance and Happy New Year!

After some changes I have problem that answer is always 40 in Fahrenheit and 15 on Calvin:

function fahrenheit(){
    var x=document.getElementById("cyfry").value;
    parseFloat(x);
    var y = ((x*1,8)+32);
    document.getElementById("wynik").innerHTML=y;
}
function kelwin(){
    var a=document.getElementById("cyfry").value;
    parseFloat(a);
    var b = (a + 273,15);
    document.getElementById("wynik").innerHTML=b;

}

You should use document.getElementById not Document.getElementById . Heres a working solution. Hope it helps!

 function fahrenheit(){ var x=document.getElementById("cyfry"); parseInt(x); x = (x*1,8)+32; document.getElementById("wynik").innerHTML=x; } function kelwin(){ var x=document.getElementById("cyfry"); parseInt(x); x = x + 273,15; document.getElementById("wynik").innerHTML=x; } 
 <div id="right"> <h2>Zamiana jednostek temperatury</h2> temperatura w <sup>o</sup>Celsjusza<br> <input type="text" id="cyfry" name="cyfry"><br> <button onclick="fahrenheit()">Fahrenheit</button> <button onclick="kelwin()">Kelwin</button> <span id="wynik"></span> </div> 

JavaScript is a case sensitive language. You have to use the uncapitalized document , not Document

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM