button not working in JSFiddle [duplicate] - javascript

This question already has answers here:
Why isn't my JavaScript working in JSFiddle?
(7 answers)
Closed 6 years ago.
This works:
<html>
<script type="text/javascript">
function lala() {
console.log("lala");
}
</script>
<body>
<button onclick="lala()">lala</button>
</body>
</html>
But having that in jsfiddle works not:
<button onclick="lala()">lala</button>
function lala() {
console.log("lala");
}
https://jsfiddle.net/clankill3r/715kha38/
Uncaught ReferenceError: lala is not defined
I saw some old answers on stackoverflow regarding this problem but those seem to be invalid those days.

Please change Load type in java script section select
No wrap - in <head>

Related

Same code works in console, but does not work in file [duplicate]

This question already has answers here:
Why does jQuery or a DOM method such as getElementById not find the element?
(6 answers)
Closed 2 years ago.
My mind has blown. I have a code wich looks (exactly) like this
const A={
B:document.getElementById("plus")
}
A.B.addEventListener("click",()=>{
console.log("1")
})
When i run it, it says "A.B" is null. I tried different versions, but still nothing. Then I copied it right into console and it worked just as expected. I put some console.log statements between object and event listener, and indeed, A and A.B both are null. Though in console they were not. What am I doing wrong?
EDIT: HTML
<!DOCTYPE html >
<html>
<head>
//some other staff
<script src="main.js"></script>
</head>
<body>
<button type="button" id="plus">+</button>
</body>
Oh man.. I got it. Should include my js file after all the DOM is loaded... Such a silly mistake after more than 4 months of practice. Laughing at myself. Thanks

Why is onclick not working in JavaScript? [duplicate]

This question already has answers here:
Why does jQuery or a DOM method such as getElementById not find the element?
(6 answers)
Closed 3 years ago.
I wrote a simple code to learn Eventhandling in JavaScript but when I click on the button "Click" the function hello() does not work, why?
html file:
<!DOCTYPE html>
<html>
<head>
<script src="hello.js"></script>
</head>
<body>
<button id="click">Click</button>
</body>
</html>
JavaScript file:
document.getElementById("click").onclick = hello;
function hello() {
alert("You Clicked!");
}
You are calling the script before the dom is generated. experiment to call the right before the tag.

How to get current iframe url in a javascript var [duplicate]

This question already has answers here:
Getting the current src of an Iframe using JQuery [duplicate]
(4 answers)
Closed 8 years ago.
Hello guys i kind of a javascript newbie.
Here is my problem
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</head>
<body>
<iframe name='frmExternal' id='frmExternal' src='www.dynamically-changing-url.com'></frame>
</body>
</html>
I want to get the current url in the iframe where the user browse along.
document.getElementsByName('frmExternal')[0].src

Why do we add "javascript:" before calling a function in HTML? [duplicate]

This question already has answers here:
When do I need to specify the JavaScript protocol?
(5 answers)
Closed 8 years ago.
I have this code:
<html>
<head>
<script>
function myFunction() {
document.getElementById("demo").innerHTML="Hello World";
}
</script>
</head>
<body>
<button onclick="javascript:myFunction()">Click me</button>
<p id="demo"></p>
</body>
</html>
if I change <button onclick="javascript:myFunction()">Click me</button> by
<button onclick="myFunction()">Click me</button>
my code runs normally.
Is that a difference between onclick="javascript:myFunction()" and onclick="myFunction()" ?
That's the javascript: pseudo-protocol, that has got lost. It's used when you have Javascript in an URL, for example:
Hi
An event attribute doesn't support the :javascript protocol, so it doesn't work there. However, it happens to become the same syntax as a label in Javascript, so the code actually still works eventhough the protocol is in the wrong place.
So, in conclusion, the :javascript shouldn't be there, and it's just plain luck that it still happens to work when it is.

Uncaught ReferenceError: myFunction is not defined [duplicate]

This question already has answers here:
Why isn't my JavaScript working in JSFiddle?
(7 answers)
Closed 9 years ago.
what on earth is wrong with this?
http://jsfiddle.net/sVT54/
<button onclick="myFunction()">Click me</button>
<p id="demo"></p>
function myFunction()
{
document.getElementById("demo").innerHTML="Hello World";
}
JSFiddle wraps your javascript code in the onLoad call of the page by default so myFunction is defined only in that function's scope. Change to No wrap - in <head> and your code will run as expected.
http://jsfiddle.net/sVT54/3/
include your JavaScript in
<Script>
function myFunction()
{
document.getElementById("demo").innerHTML="Hello World";
}
</script>

Categories

Resources