Why is my button not doing anything? [closed] - javascript

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I am trying to use HTML to call a function in an external JavaScript file. The JavaScript file is called "javascript.js".
Here is my HTML code:
<html>
<head>
<link href="style.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="javascript.js">
</script>
<script
// Calling the Google Maps API
</script>
<script>
<!-- JavaScript to load Google Maps -->
</script>
</head>
<body>
<div class="content">
<div id="googleMap"></div> <!--don't have to worry about this -->
<div id="right_pane_results">hi</div> <!--don't have to worry about this -->
<div id="bottom_pane_options">
<button onclick="todaydate()">Try It</button>
</div>
</div>
</body>
...and my JavaScript code (something I got from the internet just to test):
function todaydate() {
var today_date=new Date()
var myyear=today_date.getYear()
var mymonth=today_date.getMonth() + 1
var mytoday=today_date.getDate()
document.write("<h1>"+myyear+"/"+mymonth+"/"+mytoday+"/h1">)
}
On my webpage that I'm running locally, the button is showing, but nothing happens when I click on it. Is it something to do with my code?
Thanks in advance,
Josh

There are 2 mistakes inside the function. The > needs to be inside the quotes and there needs to be a < before the /h1.
Replace this line:
document.write("<h1>"+myyear+"/"+mymonth+"/"+mytoday+"/h1">)
with this
document.write("<h1>"+myyear+"/"+mymonth+"/"+mytoday+"</h1>")
and it should work.

I guess this has to do with the syntax error you have:
document.write("<h1>"+myyear+"/"+mymonth+"/"+mytoday+"</h1>")
note how the last quote is after the > (and you also forgot the < for the closing h1)

document.write("<h1>"+myyear+"/"+mymonth+"/"+mytoday+"</h1>")
This works!

As other answer mention syntax issue is in your document.write statement.
Beside this use getFullYear() to get year as getYear() is Deprecated.
some other practices to make your code cleaner:
Use semi-colan at end of statement
Use a good naming convention
Here is Demo
function getTodayDate() {
var todayDate=new Date();
var myYear=todayDate.getFullYear();
var myMonth=todayDate.getMonth() + 1;
var myDay=todayDate.getDate();
document.write("<h1>"+myYear+"/"+myMonth+"/"+myDay+"</h1>");
}

Related

Electron - Jquery isn't working [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
Hello,
I downloaded electron and installed it on webstorm in order to learn it. I also installed Jquery using the terminal and including in my scripts but jquery isn't working but it is recognized in my code (I haven't got any error).
My code :
const remote = require('electron').remote;
let window = remote.getCurrentWindow();
function exitGame() {
alert("test");
$('exit').click(() => {
alert("closing");
window.close();
})
}
exitGame();
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Electron Test</title>
<!-- RESET CSS -->
<link rel="stylesheet" type="text/css" href="css/reset.css">
<!-- ADDING CSS -->
<link rel="stylesheet" type="text/css" href="css/main_menu.css">
</head>
<body>
<div id="exit">Leave the game</div>
<!-- ADDING JAVASCRIPT -->
<script>
// You can also require other files to run in this process
require('./renderer.js');
require('./js/jquery.min.js');
require('./js/main_menu.js');
</script>
</body>
</html>
The jQuery alert is working but the click isn't.
Even if I replace the "exit" id by "body" it's not working...
Can you tell me what's wrong ?
Thank you very much, best regards.
exit is an id and not some element. So you need to use # before it.
$('#exit').click(() => {
alert("closing");
window.close();
})
Better way to check jQuer is wokring or not could be:
if(!jQuery)
{
alert("Not working!");
}
Alright, made it working by editing the tag with the code requiere jQuery.
Before Electron's base script tag, I added a tag with my jQuery. It seems that it wasn't loaded

javascript onclick not calling the function [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I am currently trying to learn JavaScript. I have little experience in Web. Pardon me for this question.
I am trying to use a onclick event to a function.Unfortunately, it does not load the function.
<img id="btnPartDispatch" title="sometitle" class="img-rounded" src="~/Content/Images/dashboard/PartDispatch.png" width="115" height="115" onclick="HelloWorld()" />
function is in a different file:
function HelloWorld(){
alert"('hello world')"
}
However, if I change the onclick event to alert("Hello World") it works fine.
like this:
<img id="btnPartDispatch" title="sometitle" class="img-rounded" src="~/Content/Images/dashboard/PartDispatch.png" width="115" height="115" onclick="alert('Hello World!')" />
Can someone tell me what I do wrong?
Thanks
Getting the javascript files on this way:
#section scripts
{
<script type="text/javascript">
var secondsToClosePopup = 1000 * #Functions.GetNumberOfSecondsToClosePopup();
</script>
<script type="text/javascript" src="#Url.Content("~/Scripts/jquery.multiple.select.js")"></script>
<script type="text/javascript" src="#Url.Content("~/Scripts/service.js")"></script>
}
Project is ASP.net
You have to remove the double quotes from this:
alert"('hello world')"
in
alert('hello world')
your syntax for calling alert method is wrong. You can use it like this
<html>
<body>
<button onclick="HelloWorld()">Click me</button>
<script>
function HelloWorld() {
alert("Hello World");
}
</script>
</body>
</html>
Your alert"('hello world')" will throw syntax error, because it must be alert('hello world');
Why it must be?
alert or window.alert is a function that displays browser's default dialog box.
Since its a function, you will have to add () after its name to call it, and pass necessary arguments(message) that it expects.
Hence alert('hello world').
Also as a side note, since you are binding onclick in HTML, make sure that your function is loaded before DOM is loaded. Else it will throw error.

Unable to access html elements by tag using Java script [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
My html is like this:
<!DOCTYPE html>
<html>
<head>
<title>
Event Handling
</title>
</head>
<body>
<h1> This is the chapter in which I am handling Events.The site you should visit is: </h1>
<b>here<b>
<script src="events.js"></src>
</body>
</html>
The javascript is like this:
var link = document.getElementsByTagName("a")[0];
link.onclick = MyEventHandler;
function MyEventHandler()
{
alert("ouch!");
return false;
}
It is not working, but using inling event handler I am doing it successfully. Please helop me.
You need to assign the event handler function, not the return value of calling that function.
link.onclick = MyEventHandler;
(Also note that you need to use the end tag </script> not </src>.)

How do I get the value of math.random and test it in an if statement? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
EDIT Here is new code but it is still not working for me.
HTML:
<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="mycss.css">
<script src="myjavascript2.js" type="text/javascript"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
</head>
<body>
<button onclick="myFunction()">Play</button>
<p id="test"></p>
</body>
</html>
Javascript:
var things = ['rock','paper','scissors'];
function myFunction() {
var i = Math.floor(Math.random()*things.length));
document.getElementById("test"+(i+1)).innerHTML=things[i];
}
I added this text so that it would let me post the edit, please ignore this.
You could simply replace your whole code with
var things = ['rock','paper','scissors'];
var i = Math.floor((Math.random()*things.length));
document.getElementById("test").innerHTML=things[i];
This would also let you more easily deal with other operations, like finding a winner.

Error about dynamic countdown redirect [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
I have an javascript to redirect to another page after countdown 10 seconds, but it doesn't work. Can anyone help to fix the problem?
<body>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type ="text/javascript" src ="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function() {
var number = 10;
var url = 'http://localhost/rms/index.php/';
function countdown() {
setTimeout(countdown,1000);
$('#title1').html("Redirecting in " + number + " seconds");
number --;
if(number<0) {
window.location = url;
number = 0;
}
}
countdown();
});
</script>
</head>
<body>
<div id="title1"></div>
</body>
As everybody wrote in the comments, it should work. Try to replace
<script type ="text/javascript" src ="jquery.js"></script>
with this:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.js"></script>
In this case you don't have to worry about your jquery.js file, which could be missing or corrupted.
I have one note to your code. Are you aware of that you call window.location = url; two times?
If its just a regular countdown with redirect I will put the setTimeout call in if statement which will prevent calling it twice.

Categories