+ 1

Why isn't it working?

I am pretty sure that something is wrong with JavaScript. https://code.sololearn.com/Wi5QzT90q3t5/?ref=app

12th Jun 2017, 7:17 AM
Avi_Roy
Avi_Roy - avatar
10 odpowiedzi
+ 17
Your js section should look like this: function sayHello(name) { var name = document.getElementById("rlcs"); alert("Hi, " + name.value); }
12th Jun 2017, 7:32 AM
Igor Makarsky
Igor Makarsky - avatar
+ 6
Because you make some mistakes ;P You declare a function: function sayHello(name) { alert("Hi, " + name); } ... wich take a parameter (name) to display a welcome message, but in the onclick event attribut of the button where you whant to call the function, you write: onclick ="sayHello()" So, you call it without parameter, and it's why the value of the variable 'name' in the function scope is 'undefined', and not the result of your first line of code: var name = document.getElementById("name"); ... where you do your last mistake, as you store the Html element object reference in your variable 'name', and not the text content typed by user, in addition to be undefined also, as it's called before the targeted element to select is available for JS (in code playground, the JS tab' is silently linked to the <head> part of the Html one, as it was an external JS file linked or inline embeded in a <script></script> pair tag; so it's ran before that the <body> part is parsed by the browser) ^^ You have differents solutions to fix your code; what I would do, would be: var name; // declared in global scope function sayHello() { // also for the custom function alert("Hi, " + name.value); // get the text inside the <textarea> here } // declare an anonymized function (without name reference) to initialize the value of our global var 'name' when the onload event of object window is fired (when all document is loaded and implicitly DOM ready to be accessed by JS) window.onload = function() { name = document.getElementById("name"); };
12th Jun 2017, 7:40 AM
visph
visph - avatar
+ 4
Yes... it's a normal, even if unexpected, behaviour: default 'type' attribut of <button> is 'submit', and as you are inside a <form> element, the click event try to submit the form data... You need to add type="button" to disabled this behaviour and fix the problem ;) (and delete the unnecessary 'name' parameter of your function ^^)
12th Jun 2017, 7:53 AM
visph
visph - avatar
+ 2
thanx @Igor
12th Jun 2017, 7:34 AM
Avi_Roy
Avi_Roy - avatar
0
My guess is that you shouldn't use a var called 'name' since this is a keyword in most languages
12th Jun 2017, 7:23 AM
Carl Fies
Carl Fies - avatar
0
it is still not working What I think is that I have given the Id to the textarea and JavaScript is taking that textarea as the input but we need the word that user writes in that textarea. So I need to know is there any way to assign the variable the input user gives
12th Jun 2017, 7:28 AM
Avi_Roy
Avi_Roy - avatar
0
Keep global and local scope of your variable in mind
12th Jun 2017, 7:32 AM
Carl Fies
Carl Fies - avatar
0
thanx man sorry for that I am new to coding
12th Jun 2017, 7:42 AM
Avi_Roy
Avi_Roy - avatar
0
Is the code opening Google in anyone else's phone
12th Jun 2017, 7:46 AM
Avi_Roy
Avi_Roy - avatar
0
ok thanx
12th Jun 2017, 7:55 AM
Avi_Roy
Avi_Roy - avatar