Вопрос

My code:

<button type="button" onclick="ayee()" value="click" />
                    <script>                        
                    function ayee(){
                        confirm("Ready to play?");
                        var age = prompt("How old are you?");
                        if (age >= 18) {
                            document.write("Let's get started then!");
                        }else{
                            document.write("You're under 18? Be careful out there....");
                        }
                    }
                    </script>

Basically, im using document.write() to write the response of the JS, but I want to push the response to a certain Division or area, so I can keep the format of the rest of the page.

Это было полезно?

Решение

Don't use document.write. Instead, create a div where you want the output to go and give it an id:

<div id="result">I will be updated</div>

Then in your JS code, use:

function ayee() {
    confirm("Ready to play?");
    var age = prompt("How old are you?"),
        // Get a reference to the element we want to update
        el = document.getElementById('result'),
        message;

    // Check the age and set the message variable based on that
    if (age >= 18) {
        message = "Let's get started then!";
    } else {
        message = "You're under 18? Be careful out there....";
    }
    // Update the content of the element with the message
    el.innerHTML = message;
}

Другие советы

First put an id attribute on the division to which you want to write the data

<div id="div-id"></div>

Then use following line to change its content

document.getElementById("div-id").innerHTML="Text you want tot write here"
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top