javascript onclick not calling the function [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 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.

Related

Only first button works. Not sure if its the script. It might be I'm missing a rule [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 2 years ago.
Improve this question
I know I'm a beginner at this, but I really need to learn the rules. I'm trying to make a simple example of javascript. Its a box with three buttons, but for some reason only one of them works. They do have different ID types, so there must be another rule I don't know.
<!DOCTYPE html>
<html>
<head>
<title>Javascript box</title>
</head>
<body>
<p>Press buttons to change the box</p>
<div id="box" style="height:100px; width:100px; background-color: limegreen; margin:50px"></div>
<br>
<button id="less">less</button>
<button id="more">more</button>
<button id="none">Reset</button>
<script type="text/javascript">
document.getElementById("less").addEventListener("click", function(){
document.getElementById("box").style.height = "25px";
});
document.getElementByID("more").addEventListener("click", function(){
document.getElementById("box").style.height = "150px";
});
document.getElementById("none").addEventListener("click", function(){
document.getElementById("box").style.height = "100px";
});
</script>
</body>
</html>
For the second addEventListener, there's a typo.
In document.getElementByID, the D should not be capitalised.
A good way to debug these issues is to look at the console in developer tools.

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>.)

calling javascript function from body of html [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a javascrip[t function that is saved in a file (test.js). I want to call the function from within the body of the html file
something like
<script>
load drawLogo("text" val1, val2, val3);
</script>
(since this function will be called on different pages with different values)
Right now I am having to put that call at the bottom of the main function drawLogo() which means I can't customize it.
<script src="text.js" type="text/javascript"></script>
if it is in same function then make it like:-
onclick="func()" or onmmousehover="func()"
define function func() in the same file within script tag
Just put the function name and the parameters that receive this, dont forget first call the js file:
test.js:
function drawLogo(paramText,value1,value2,value3){
//Do something
}
html:
<head>
<script src="test.js" type="text/javascript"></script>
<!--you can put this in the head or in the body-->
<script>
drawLogo("some","value","for","example");
</script>
</head>
<body>
<!--you can put this in the head or in the body-->
<script>
drawLogo("some","value","for","example");
</script>
</body>

Why is my button not doing anything? [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
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>");
}

both program have nearly same code but output is different [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
<!DOCTYPE html>
<html>
<body>
<h1>My First Web Page</h1>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction()
{
document.write("Oops! The document disappeared!");
}
</script>
</body>
</html>
The above code is showing output like below
My First Web Page
Try it
After clicking Try it button I got output like below
Oops! The document disappeared!
In internet i found the reason for disappear is If I execute document.write after the document has finished loading, the entire HTML page will be overwritten.
but my doubt is when document has finished loadind...........i am not gettimg it.
Then I modified the above code like below
<!DOCTYPE html>
<html>
<body>
<h1>My First Web Page</h1>
<button onclick="myFunction()">Try it</button>
<script>
function myfunction()
{
document.write("My First JavaScript");
}
</script>
</body>
</html>
but the above code is showing output like below
My First Web Page
Try it
After pressing Try it button it is not giving any output.I don't know why these 2 program are working like this.help me..................
Second one is failing because you have a small f in myfunction and call it with a big F
In Chrome, if you open Developer tools ( Cntrl-Shift-J ) and go to the console it will show you the javascript error.
The problem in the second page is that you are calling a function called myFunction.
But the function that you have is called myfunction, the F is in Uppercase.
so the javascript error is:
Uncaught ReferenceError: myFunction is not defined
JavaScript is case sensitive. You define
myfunction
But call
myFunction
Note the F.
In your second function, myfunction, F should be CAPS or at onclick=myFunction() F should be small. Check your console window. No output is because of javascript errors

Categories