user2713996
user2713996

Reputation: 147

html/javascript scripting problems

I testing out some things, combining html and javascript. Right now I'm trying to make a button do something when the user clicks it.

Fx

<head> 
<script type="text/javascript">
function popup() 
{
confirm ("Welcome!");
}
</script>
</head>

Plus the button:

 <body> 
 <button onclick="popup()">Try it</button>
 </body>

This works out very well and the popup windows appears. Now when I for example want to print a line when clicking the button, I'm just replacing "confirm" with "console.log", wich doesnt work...

What am i doing wrong, and how would i make the button print a simple line of text?

Thanks!

Upvotes: 0

Views: 44

Answers (3)

ffflabs
ffflabs

Reputation: 17511

I reccomend you use a DOM manipulation library like jQuery to avoid setting events and behaviors manually.

This means doing something like:

<html>
    <head>
        <script src="https://code.jquery.com/jquery-2.1.0.min.js"></script>
        <script>
            jQuery(document).ready(function() {
                jQuery('#mybutton').click(function() {
                    jQuery('#mytext').append('This is a line of text');
                });
            });
       </script>
    </head>
    <body> 
         <button id="mybutton">Try it</button>
            <div id="mytext"></div>
    </body>
</html>

I took your question literally. This will append text to the DOM. Logging to console has no effect on the DOM, but at is was already said, you can inspect it with F12.

Upvotes: 1

nicael
nicael

Reputation: 19024

console.log is for debugging. To print something on page, use

<head> 
<script type="text/javascript">
function popup() 
{
document.getElementById("div").innerHTML="Welcome!"
}
</script>
</head>
<body> 
 <button onclick="popup()">Try it</button>
 <div id="div"></div>
 </body>

Upvotes: 1

Amit Joki
Amit Joki

Reputation: 59292

Press F12 and click on the console tab.

There you'll be able to see the result.

console.log() is used for debug purpose and logging purpose. It is not used for pronting a line.

Upvotes: 2

Related Questions