Activate href by javascript [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 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:

Related

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

How to make a Go url bar in html+css [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 7 years ago.
Improve this question
Im a noob at js, and I would like to make a go bar like at the top of browsers in html and js. It will go to the url in the box when the button is pressed nothing more, nothing less. Heres about what i want:
In javascript you just have to add an event listener in the "Go" button.
Then when you click the button, you just have to redirect the url according to the text field value.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<input type="text" id="field" />
<button id="myBtn">Go</button>
<script type="text/javascript">
document.getElementById("myBtn").addEventListener("click", function(){
var url = document.getElementById("field").value;
window.location.href = url;
// OR
window.open(url);
});
</script>
</body>
</html>
With this code, if you enter a value in the text field and click the button. The url will add your value at the end.
For example if you are testing on the url localhost/ and you enter "test", javascript will redirect you to localhost/test.
To redirect correctly you must write "http://" or "https://" at the beginning

How create Html button that run 2 linked js files? [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
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>

Categories

Resources