-4

I want to know that in the following code, when I reference an element by (document.getElementById) and make it equal to a variable for the validation purpose why I can't use the name instead of name1 in javascript.

function validation() {
  name1 = document.getElementById('name');
  if (name1.value == "") {

    alert('this field cannot left empty');
    return false;
  }
}
<form id="form" action="message.html" method="post" onSubmit='return validation()' />
<input type="text" placeholder="enter your name" id='name' />
<input type="text" placeholder="address" id='address' />
<input type="tel" placeholder="telephone" id="telephone" />
<input type="submit" value="submit">
H. Pauwelyn
  • 13,575
  • 26
  • 81
  • 144
Ali Raza
  • 15
  • 4
  • Try using console... F12 – Akxe May 07 '17 at 08:15
  • are you saying you can't use `name=document.getElementById('name');` ? – lu5er May 07 '17 at 08:16
  • Yes in this situation I can't use the variable name same like element id. – Ali Raza May 07 '17 at 08:18
  • 1
    use `var name=document.getElementById('name');` – lu5er May 07 '17 at 08:19
  • @Ali Raza first you need to add `var` before name1. – Sumit Surana May 07 '17 at 08:19
  • My question is that why var name=document.getElementById('name'); this can't work. – Ali Raza May 07 '17 at 08:21
  • 1
    This works man! Try it and let us know.. – lu5er May 07 '17 at 08:21
  • And in javascript you don't need to specify a variable if you don't put var before a variable name javascript will still use it as a variable. – Ali Raza May 07 '17 at 08:22
  • @AliRaza I guess this stackoverflow question should answer your doubts http://stackoverflow.com/questions/6881415/when-is-the-var-need-in-js – Sumit Surana May 07 '17 at 08:29
  • Per https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var the reasons for declaring variables with var: variables declarations get creation priority over undeclared variables, undeclared variables are always global, undeclared variables may be deleted. In sum, to avoid potential problems it is usually better to declare variables. – slevy1 May 07 '17 at 08:57

3 Answers3

2

After adding var to name1 the code will start executing. After adding var you can assign the document.getElementById('name') to name also and it would work. You can run the below snippet for reference.

function validation(){
    var name=document.getElementById('name');
    if(name.value== ""){
        alert('this field cannot left empty');
        return false;
     }
}
<form id="form" action="message.html" method="post"
 onSubmit='return validation()'>
    <input type="text" placeholder="enter your name" id='name' />
    <input type="text" placeholder="address" id='address' />
    <input type="tel" placeholder="telephone" id="telephone" />
    <input type="submit" value="submit">
</form>
Sumit Surana
  • 1,554
  • 2
  • 14
  • 28
0

Try using this..

You forgot to declare the variable. If you are using a variable for assignment you must declare it before using.

For more reference you can look up this popular thread in SO, What is the purpose of the var keyword and when to use it (or omit it)?

function validation(){
    var name = document.getElementById('name');
    if(name.value== ""){
        alert('this field cannot left empty');
    return false;
   }
}
Community
  • 1
  • 1
lu5er
  • 3,229
  • 2
  • 29
  • 50
  • Thanx a ton so it is always good to declare the variable . I thought that if i don't declare the variable javascript will still use it as a variable . – Ali Raza May 07 '17 at 08:28
  • @AliRaza For clarifications regarding `var` you can consult http://stackoverflow.com/questions/1470488/what-is-the-purpose-of-the-var-keyword-and-when-to-use-it-or-omit-it. It's always good practice to declare a variable before using it. – lu5er May 07 '17 at 08:29
0
var name=document.getElementById('name');

You should use var before an element name.

Ashkar
  • 712
  • 6
  • 17