How create Html button that run 2 linked js files? [closed] - javascript

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
What do I need to do in order to execute the code inside my two external JS files when the button shown below is clicked?
<html>
<head>
<title>Vending Machine</title>
</head>
<body>
<script src="script.js"></script>
<script src="script2.js"></script>
<button type="button" onclick="">Press the Button</button>
</body>
</html>

Inside your javascript file there should be a function with name myFunction for example
<button onclick="myFunction()">Try it</button>

You should call the 2 functions from the files inside your click
<button type="button" onclick="function1(); function2();">Press the Button</button>

Related

Why does my simple script tag break the following script tag? [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 7 months ago.
Improve this question
I am tearing my hair out over this. Why is foo() undefined when I click the button in this script?
<html>
<body>
<script type="text/javascript" src="./app2.js"/>
<script">
function foo() {
console.log('foo...');
}
</script>
<button type="button" onClick="foo()" id="testbutton">Click!</button>
<button type="button" onClick="hello()">Click hello!</button>
</body>
</html>
but not if I remove the first script tag?
<html>
<body>
<!-- <script type="text/javascript" src="./app2.js"/>-->
<script>
function foo() {
console.log('foo...');
}
</script>
<button type="button" onClick="foo()" id="testbutton">Click!</button>
</body>
</html>
My app2.js is just
function hello() {
console.log('hello');
}
I have tested in Chrome and Safari on macOS. The hello function works as expected.
Auto closing tags are used in React JSX and not in vanilla HTML
Replace
<script type="text/javascript" src="./app2.js"/>
with
<script type="text/javascript" src="./app2.js" ></script>

Activate href by javascript [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 1 year ago.
Improve this question
I want to click on the first href element by javascript. But I have no idea how to reach website.com webpage with DOM. Can you tell me pls?
<!DOCTYPE html>
<html>
<body>
<td>hello</td>
<td>hi</td>
<script src=""></script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<td><a id="yourid" href="https://website.com" tabindex="-1">hello</a></td>
<td>hi</td>
<script>
function myFunction() {
document.getElementById("yourid").click();
}
</script>
</body>
</html>
give the first href an id and then paste this code in your script tag:

Show ONLY JavaScript Disabled message [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I want to show a message that says "Please enable JavaScript to use this site" in the case that JavaScript is disabled, and nothing else to be displayed when JavaScript is disabled.Please do not reply ONLY about the <noscript> tag or code like this.
Use <noscript>, with a <style> rule inside the <noscript> that hides everything else using the body > *:not(noscript) selector:
<head>
<noscript>
<style>
body > *:not(noscript) {
display: none;
}
</style>
</noscript>
</head>
<body>
<noscript>
<p>Please enable JavaScript</p>
</noscript>
</body>

How to show simple json file data on html Page? [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 2 years ago.
Improve this question
I have a json file which shows a number say "12925.09".
How can I show this number on my HTML page?
<html>
<title>HTML Tutorial</title>
<body>
Number =
</body>
</html>
Thanks
Here's one way. Run the script below, comments are within the code.
//Your possible (simple) json data
var numbers = {"number":10}
//Get number div by its id
var numberDiv = document.getElementById("numberDiv");
//Target number divs innerHTML and set it to the number value (10) in your numbers object
numberDiv.innerHTML = numbers.number;
<html>
<title>HTML Tutorial</title>
<body>
<div id="numberDiv"></div>
</body>
</html>

Binding .this to an HTML element [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 6 years ago.
Improve this question
I've been trying to get this "this" to work, to bind it to an/any HTML element that the function is applied to. I'm trying to keep it universal to make it possible to apply the same function to multiple HTML elements.
Note:
I don't want to mess with ids nor classes and such since I want to keep the function as universal as possible.
<!doctype html>
<html>
<body>
<input type="button" value="Old" onClick="Change(this);">
<script type="text/javascript">
function Change(){
this.innerHTML = "New";
};
</script>
</body>
</html>
innerHTML won't work in this case. Being that this is an input, try using value instead.
var input = document.querySelector('input');
input.addEventListener('click', Change);
function Change(){
this.value = "New";
this.removeEventListener('click', Change);
}
<input type="button" value="Old" />
The problem is that when the page hits the input the Change function is not defined. Try the following:
<!doctype html>
<html>
<body>
<script type="text/javascript">
function Change(){
this.value = "New";
};
</script>
<input type="button" value="Old" onClick="Change.call(this);">
</body>
</html>
Note that I have changed the input's onClick to Change.call(this). And that the script is loaded before the control.
You can see a working fiddle here. Note that the JavaScript settings is configured so that the script is loaded in the head (Hit the cog in the JavaScript section).

Categories

Resources