Jump to content

Um, js help :DD pls

Zuccers
Go to solution Solved by mariushm,

Well, this would work:

 

<html>
<body>
<script type="text/javascript">
var toggled = false;
        function toggle() {
        if(!toggled) {
        toggled = true;
        let d = new Date(Date.now());

        document.getElementById("tgl").innerHTML = d.getHours() + ':' + d.getMinutes() + ':' + d.getSeconds();

        return;
        }
        if(toggled) {
        toggled = false;
        document.getElementById("tgl").style.display = "block";
        return;
        }
        } 
		
</script>
<div id="tgl">text</div>
<input name="go" type="submit" onclick="toggle();"; />
</body>
</html>

 

So, i want to make a function "clock"! It's a button and onclick i would display my function, but as i am newbie on js i really need your help :)
i've got this:

       

Quote

var toggled = false
        function toggle() {
        if(!toggled) {
        toggled = true;
        var  d = new Date();
        var  n = d.getHour();
        document.getElementById("tgl").innerHTML = n;
        return;
        }
        if(toggled) {
        toggled = false;
        document.getElementById("tgl").style.display = "block";
        return;
        }
        }

How do i make that  button display the hour?

Link to comment
Share on other sites

Link to post
Share on other sites

Well, this would work:

 

<html>
<body>
<script type="text/javascript">
var toggled = false;
        function toggle() {
        if(!toggled) {
        toggled = true;
        let d = new Date(Date.now());

        document.getElementById("tgl").innerHTML = d.getHours() + ':' + d.getMinutes() + ':' + d.getSeconds();

        return;
        }
        if(toggled) {
        toggled = false;
        document.getElementById("tgl").style.display = "block";
        return;
        }
        } 
		
</script>
<div id="tgl">text</div>
<input name="go" type="submit" onclick="toggle();"; />
</body>
</html>

 

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, mariushm said:

Well, this would work:

 


<html>
<body>
<script type="text/javascript">
var toggled = false;
        function toggle() {
        if(!toggled) {
        toggled = true;
        let d = new Date(Date.now());

        document.getElementById("tgl").innerHTML = d.getHours() + ':' + d.getMinutes() + ':' + d.getSeconds();

        return;
        }
        if(toggled) {
        toggled = false;
        document.getElementById("tgl").style.display = "block";
        return;
        }
        } 
		
</script>
<div id="tgl">text</div>
<input name="go" type="submit" onclick="toggle();"; />
</body>
</html>

 

Yeah, it does work, but now the button only has one function onclick = show time, i wanted to make this a toggle button, so... i need to somehow to "turn it off"

 

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

The button just calls the function. It's there just to make it easy for you to see the function being executed.

Your problem was that you couldn't display text in the div, now it does.

It's up to you to fix everything else, like get the function to be executed every half a second or every second to have the time updated

 

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, mariushm said:

The button just calls the function.

Your problem was that you couldn't display text in the div, now it does.

It's up to you to fix everything else, like get the function to be executed every half a second or every second to have the time updated

 

:/ Sad thing is, i don't know how to, but still thanks :)

Link to comment
Share on other sites

Link to post
Share on other sites

<html>
<body>
<script type="text/javascript">
		
		function updateTime() {        
		let d = new Date(Date.now());
        document.getElementById("tgl").innerHTML = d.getHours() + ':' + d.getMinutes() + ':' + d.getSeconds();
        return;
		}
		
		window.setInterval(updateTime,500);
</script>
<div id="tgl">text</div>
</body>
</html>

 

Link to comment
Share on other sites

Link to post
Share on other sites

I would suggest you to edit the headline of the topic. 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×