+ 3
How do you declare a variable in Html 5?
40 odpowiedzi
+ 23
Unfortunately html doesn't have variables
+ 14
you don't declare variable in html...
you can only declare variables in css (kind of) or in javascript ^^
assuming you meant "in javascript":
by using 'var', 'let', or 'const' keywords...
+ 7
In html5 there are no variables
+ 7
The <var> HTML element represents the name of a variable in a mathematical expression or a programming context. It's typically presented using an italicized version of the current typeface, although that behavior is browser-dependent..
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/var
+ 6
Hacker Gentleman [ Follow Me ] no, html compile and store page as DOM (Document Object Tree) model, wich is only accessible throuhj DOM api in javascript... all ids names are assigned as global 'window' object keys to the related html element id in the DOM... unless they are implicitly or explicitly declared by js script ^^
only 'window' keys wich are also valid javascript variable name identifier can be accessed AS global variables ('window' properties): window["my-id"] cannot be accessed as variable, because "my-id" is not a valid js variable name (parsed as "my" minus "id")...
also, if you use "valid_name" as id, but you also assign a value to it somewhere in your script (implicit variable declaration in js) or declare (explicitly using 'var', 'let', or 'const') global variable with same "valid_name" or even declare global function with same "valid_name", the html element still exist in memory (DOM) but is no more accessible through 'window' property (wich now refer to the javascript variable/function)
+ 5
Vasiliy you doesn't really declare variable in html by using ids... rather, in javascript, 'window' global object receive as properties keys the ids reference element in DOM... but NOT "all ids are global variables": only ids that follow valid variable identifier name rules in js can be accessed AS global variables (ie: id="my-id", cannot be accessed as a variable name in js, but is available as window["my-id"]) unless you override value by implicitly or explicitly declaring global variable in js ^^
+ 5
Like this 😎😛
https://code.sololearn.com/W8FVN3fCd6hL/?ref=app
+ 4
The <var> tag is used to highlight the variables of computer programs.
+ 4
Hacker Gentleman [ Follow Me ] attributes are not variables... all the more kind of properties, wich you cannot define as you want ^^ (only supported attributes are valid html attributes -- in html 5 custom attributes can be defined by prefixing them with "data-")
html is not exactly "from xml family"... html is from sgml family (wich is near from xml, but not same) and xml nor sgml nor html "can be accessed by many languages" but can be parsed by any programming language: it's up to the parser to build an object model from the parsed xml/sgml/html: none have variables definition/declaration by himself, only programming languages have ;P
+ 4
Hacker Gentleman [ Follow Me ] I'm agree that "nothing cant work without variables", but that's not the point of OP question wich ask: "How do you declare a variable in Html 5?"
under the hood html engine are done with some programming languages wich use variables obviously... but html by itself is only a text only markup language wich code data structure of web document, and doesn't have "variables" nor "variable declaration": only attributes syntax inside tags... 'id' is one of the generic attriibutes available, and id values are not variables unless the html engine assign them as 'window' properties of same keys (names) and that names becomes unused by the embeded script as well as being valid variable name identifiers ^^
+ 3
All tag IDs are global variables.
+ 3
A variable is a name assigned to a memory location in a computer that stores specific data. Variables are in every programming language, even the low-level assembly language.
The variable name is an identifier, so it follows the same rules.
+ 3
visph give an example code to make it clearer what you are talking about
+ 3
Akosua, HTML/HTML5 doesn't have variables
You can declare variable in JS (JavaScript).
+ 2
html5
id = "globalVariable" ☺️
CSS
/* Declaring a global variable */
:root {
--color: red;
}
/* Using a variable */
body { /* fallback - green */
background: var(--color, green);
}
https://code.sololearn.com/WnnXLKBtBfjZ/?ref=app
+ 2
Hacker Gentleman [ Follow Me ] being "like" something is not being something...
+ 2
Hacker Gentleman [ Follow Me ] feel free to believe what you want: I've tried to explain why there's no variable declaration in html, but now I will no more loose time into that subject ^^
+ 2
HTML is based on the Standard Generalized Markup Language (SGML), at CERN. Eleven of these elements still exist in HTML4.
Then Attributes began to appear, the development of which was significantly influenced by the new TABLE tag. Finally, there is a new STYLE tag so that you can "get rid of the proliferated" attributes. And in the end, CSS came to the rescue, which is an integral part of html.
visph hopefully you won't deny having variables in CSS? THEREFORE, I emphasized my answer on this, not knowing what exactly the author of the question means. I was not going to prove that it is primary: "an egg or a chicken", but as far as I understand everything that was said here are objects, including VARIABLES, and objects are stored in variable memory locations ☺️ Perhaps everything is much simpler, and we are talking about the already mentioned tag <var>. In any case, there is no point in arguing, about this, everyone expressed their opinion and stayed with their own ☺️
+ 2
😅🤣😂👍👍Nice question!!!