Error defining getElementById?

2

I have an element where the id attribute is an integer. I'm trying to define, but I can not.

I'm doing this:

var 1 = document.getElementById(1).value

Is this correct?

    
asked by anonymous 18.11.2016 / 15:29

4 answers

8

The problems are:

  • Variables can not be composed only of numbers var 1; var 2;
  • Parameter in getElementById must be string
  • Extras:

    • HTML4.01 : The ID attribute can never be repeated, and should start with letters and should not contain spaces

    • HTML5 : It is more permissive, but still not accepts spaces.

    • IDs should never be repeated on the same HTML page

    The problem is where to start learning

    The problem of the question is not its error, but it is a common problem of those who are beginning to learn, since they often invent their own head or exit using a random function or code . For all popular technologies there is always at least official (or unofficial) documentation, the same goes for JavaScript that is widely used, so do not try to use it without following at least one example that is in the documentation, if you have difficulties read the details of the own documentation about the function you are currently trying to use, then learn the basics first, as it is no use skipping steps.

    In JavaScript start with something like: link

    To learn how to use and declare variables read in DOC:

    To learn about using getElementById :

    Other links within the doc useful to get started:

      

    Most javascript docs have a problem, especially in Portuguese, but still to learn the basics the Mozilla link serves well

    To solve

    To solve you can do something like:

    document.getElementById("item_1").value;
    

    And change the HTML to something like (although HTML5 is more lenient about ID ):

    Of course you can use numbers in the ID being HTML5, as I said, but in case I do not know which browsers you want to run and even if it works in HTML4, you can still get in the way.

    An example:

    window.onload = function() {
        var item1 = document.getElementById("item_1");
        var button = document.getElementById("enviar");
      
        button.onclick = function() {
            console.log(item1.value);
        };
    };
    <input type="text" value="Olá mundo" id="item_1">
    
    <button id="enviar">Enviar</button>
        
    18.11.2016 / 15:42
    5

    Your problem is in the name of the variable. Variables with numbers at the beginning of the name are invalid.

    • div1 functions as variable name
    • 1div or 1 does not work as variable name

    The error this gives says this, "An unexpected number appeared":

      

    (index): 45 Uncaught SyntaxError: Unexpected number

    The .getElementById accepts a variable or string as an argument. You can use a number, which is accepted in HTML5, but the correct thing is to use a string.

    Example:

    var um = document.getElementById(1);
    um.style.color = 'red';
    <div id="1">1</div>
    <div id="2">2</div>
        
    18.11.2016 / 15:49
    5

    The problem is that the name of your variable is an integer, you should never do this, because even if it did (I'm glad it did not) it would bring a lot of problems, just like the id must be passed as a string inside the getElementById() , does the following:

    var val_1 = document.getElementById("1").value;
    alert(val_1);
    <input id="1" value="heya">

    And as mentioned in Virgilio's answer, you should also avoid using only a number as id, nor should you put the number at the beginning of the id name, because if you want to style (CSS) using id as selector you will not be able to, eg:

    #1 {
      background-color: red;
    }
    <input id="1" value="heya">

    Example that works:

    #a1 {
      background-color: red;
    }
    <input id="a1" value="heya">
        
    18.11.2016 / 15:31
    3

    It's not a good practice to id ( read ) of any tag only with a number always enter the initial one letter ( example: a1 , b1 , etc.), code has the problem of putting a number as variable > can not in any language, it follows a standard one letter and then can have numbers , just below has the explanation of creating variables with php and javascript ) and lastly the getElementById is a text ( getElementById("t1") );

    To function basically without much change:

    var numero = document.getElementById("1").value;
    console.log(numero);
    <input type="text" value="100" id="1" name="t1" />

    A correct way: id was added a t letter and after a number (t1) :

    var numero = document.getElementById("t1").value;
    console.log(numero);
    <input type="text" value="100" id="t1" name="t1" />

    How to define a variable?

    Javascript

    The JavaScript language is case-sensitive. This means that a variable name, such as myCounter, is different from the MYCounter variable name. Variable names can be of any length. The rules for creating legal variable names are as follows:

    • The first character must be an ASCII letter (uppercase or lowercase) or an underscore character (_). Note that a number can not be used as the first character.
    • Subsequent characters must be letters, numbers, or underscores (_).
    • The variable name should not be a reserved word .

    Reference and Copyright - MSDN Microsoft

    PHP

    Variable names follow the same rules as other labels in PHP. A valid variable name begins with a letter or underscore, followed by any number of letters, numbers, or underscores. In a regular expression, it could be represented as: [a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]**

    Reference and Copyright - PHP Site .

    Even though a setting for PHP follows the basic naming for variable names in programming.

    These two texts that explain how to create names for variables will allow a better understanding of how to define names including elements of , because they are also names of screen variables, careful with repetition of names that can be a major development disorder and use those nomenclatures that defined the creation of variable names.

    References:

    18.11.2016 / 15:33