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>
Related
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:
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
<!DOCTYPE html>
<html>
<head>
<title>my site</title>
<script>
// javascript code here
// generates a random number, X, from 1 to 10 each time this page is requested
// displays X number of "blue-object"
</script>
<style>
/*css if needed */
</style>
</head>
<body>
<div class = "blue-object" style="background:blue; margin:20px">This is an instance of a blue object</div>
<div class = "blue-object" style="background:blue; margin:20px">This is an instance of a blue object</div>
</body>
</html>
So currently the page above is static. It always display 2 objects of the "blue-object" class.
I need to make it dynamic. So for the sake of simplicity, the number these objects that gets displayed is a randomly generated number. So how can that be done?
Here you go :
<!DOCTYPE html>
<html>
<head>
<title>my site</title>
<style>
div{
background-color:blue;
margin:20px;
}
</style>
</head>
<body id="main">
<script>
var desiredText="Hey there! change me ;)";
//You can just change the variable to your desired text and that's it :)
var randomRepeater=function(desiredText){
var iterator=Math.floor(Math.random() * 10);
console.log("will create "+iterator+" divs this time :) ");
for(var i=0;i<iterator;i++){
var div = document.createElement("div");
div.innerHTML = desiredText.toString();
document.getElementById("main").appendChild(div);
}
}
randomRepeater(desiredText);
</script>
</body>
</html>
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 5 years ago.
Improve this question
I want make a that auto scrolls and I want to be able to add paragraph tags to it using javascript.
Any help would be appreciated
var div = document.getElementsByTagName('div'),
p = document.createElement("p");
p.innerHTML = 'I am p Tag';
div[0].append(p);
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
</head>
<body>
<div> I am div Tag</div>
</body>
</html>
Depending on properties of your div like Id, name, class, u can also use
document.getElementById etc.
Using jQuery plugin for javascript, you can achieve this like that:
var myDiv = $("#IdOfYourDiv");
var paragraph = document.createElement("p");
paragraph.textContent = "something";
myDiv.append(paragraph);
but next time, pelase insert your html/javascript code, nobody have a crystal ball to know how you are writing your code to help you ;)
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
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>