Error set getElementById?

Asked

Viewed 544 times

2

I have an element where the attribute id is a whole. I’m trying to define, but I’m not getting.

I’m doing like this:

var 1 = document.getElementById(1).value

That is correct?

  • 3

    No, this you cannot set a number to variable in any language and in getElementById("1").value is a text inside the parameter. It has how to tag?

4 answers

8

The problems are:

  1. Variables cannot be composed only of numbers var 1; var 2;
  2. The parameter in getElementById must be string

Extras:

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

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

  • Ids should never repeat themselves on the same HTML page

The problem is where to start learning

The problem with the question is not its mistake, but it is a common problem of who is beginning to learn, because they often invent the own head or leave using a function or code randomly. For all popular technologies there is always a documentation at least (official or unofficial), the same goes for Javascript that is widely used, so do not try to use without following at least one example that is in the documentation, if you have difficulty reading the details of your own documentation about the function you are trying to use at the moment, then learn the basics first, because it is no use skipping steps.

In Javascript start with something like: https://developer.mozilla.org/en-US/docs/Web/JavaScript

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

To learn about the use of getElementById:

Other useful links within the doc to get started:

Most of the javascript "Docs" have some problem, mainly in Portuguese, but still to learn the basics the Mozilla link meets well

To solve

To solve can do something like:

document.getElementById("item_1").value;

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

Of course you can use numbers in the ID being HTML5, as I said before, but in case I don’t know which browsers you want to run and even if it works in HTML4, you might still get stuck.

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>

5

The problem is that the name of your variable is an integer, you should never do this, because if it did (I’m glad it doesn’t) it would bring a lot of problems, just as the id should be passed as string within the method 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 reply, you should also avoid using only one number as an id, nor should you put the number at the beginning of the id, because if you want to style (CSS) using id as selector you will not succeed, ex:

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

Example that works:

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

5

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

  • div1 works as a variable name
  • 1div or 1 does not function as a variable name

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

(index):45 Uncaught Syntaxerror: Unexpected number

The .getElementById accepts a variable or string as argument. You can use a number, which is accepted in HTML5, but the correct 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>

3

It is not good practice to put the id (read) of any tag only with a number always put the initial one letter (example: a1, b1, etc.), in the code has the problem of putting a number as a variable (this can not in any language, follows a standard one letter and then can have numbers, below is the explanation of creation of variables with and ) and finally the Conference within the getElementById is a text (getElementById("t1"));

To work basically without many changes:

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

One correct way: The id a letter has been added t 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 distinguishes between upper and lower case. This means that a variable name, such as myCounter, is different from the variable name Mycounter. 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 cannot be used as the first character.
  • Subsequent characters shall be letters, numbers or underscores (_).
  • Variable name must 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 starts with a letter or underline, followed by any number of letters, numbers or underscores. In a regular expression, it could be represented like this: [a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]**

Reference and authorship rights - PHP Site.

Even though it’s a definition for PHP follows the basic nomenclature for creating 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, are also screen variable names, care with repetition of names that can be a major disorder in the development and use these nomenclatures that define the creation of variable names.

References:

Browser other questions tagged

You are not signed in. Login or sign up in order to post.