简体   繁体   中英

How do I change from an alert message to a regular text output?

I have a code snippet of my function that will output the total calculated GPA which is:

alert("GPA =  " + eval(gpa));

However, I do not want it outputted as an alert.

I want it outputted in regular text.

So how do I change that so in HTML it will just output it in regular text instead of an alert?

Is there a way to just replace the

alert

part of the code with something else?

First create a div where you want to show text

<div class="text-message" style="display:none;"></div>

And change alert message with

$(".text-message").text("GPA =  " + eval(gpa)).show();

 OR to animate it

$(".text-message").text("GPA =  " + eval(gpa)).fadein();

You could try something like following with HTML + JavaScript .

 function gpa(param1 = 1, param2 = 2){ let resultGPA = param1 + param2; console.log("GPA = " + resultGPA); document.getElementById('evalElement').innerHTML = resultGPA; }
 <!DOCTYPE html> <html> <head> </head> <body> <button name="btn" onclick='gpa()'>Calculate GPA</button> <p id = "evalElement"></p> </body> </html>

This should help you achieve your goal!

If your html element id is gpa , then in your JavaScript -file use following:

document.getElementById("gpa").innerHTML = "GPA = " + eval(gpa);

Step 1

Let's change your alert code to:

myAlert("GPA =  " + eval(gpa));

Step 2

Implement myAlert

function myAlert(content) {
    let context = document.getElementById('message-block');
    context.display = 'block';
    context.querySelector('.content').innerHTML = content;
}

Step 3

Create the structure:

<div id="message-block" style='display: none;'>
    <div class="content"></div>
    <input type="button" value="Close" onclick="this.parentNode.display = 'none';"
</div>

 let gpa = "'a'+'b'"; function myAlert(content) { let context = document.getElementById('message-block'); context.style.display = 'block'; context.querySelector('.content').innerHTML = content; } myAlert("GPA = " + eval(gpa));
 <div id="message-block" style='display: none;'> <div class="content"></div> <input type="button" value="Close" onclick="this.parentNode.style.display = 'none';"> </div>

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