简体   繁体   中英

Button not linking to input field with javascript

I want the button to run the code in the corresponding function field, but I don't believe I have it set up correctly. The string or numbers entered in the field should be processed and then the answer should be printed out. The last 'else' statement is simply to see if it runs the code, which it did, but I could enter both numbers and strings and the first statement would be printed "This is not a year". I also am not sure that I formatted the second 'else/if' statements parameters of (enter % 4 && enter % 100 && !enter % 400) correctly. Any help would be appreciated as I have tried to resolve this on my own and have progressed slowly, so any help is much appreciated. Thank you!

    <!DOCTYPE html>
    <html>
    <head>
    <link rel="stylesheet" href="style.css" />
    <script src='script.js'></script>
    <title></title>
    </head>
    <body>
    <!--leap year calculator-->
    <h3>Is it a leap year?</h3>

    <form action="">
        Year:<input type="text" name="enter" id="enter"> <br>
    </form>
    <script>function YOLO(enter){
    if (iNaN(enter)){
    console.log("This is not a year");
    }
    else if (enter % 4 && enter % 100 && !enter % 400){console.log("no");
    }
    else if(enter % 4){console.log("yes");
    }
    else {alert("working")}
    };

    var Id= document.getElementById("enter");
    </script>
    <button onclick="YOLO(Id)">Enter</button>

    </body>
    </html>

You must work with value property of element. Something like this:

<button onclick="YOLO(document.getElementById('enter').value);">Calculate</button>

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