Can I add a custom attribute to an HTML tag like the following?
<tag myAttri="myVal" />
Can I add a custom attribute to an HTML tag like the following?
<tag myAttri="myVal" />
You can amend your !DOCTYPE declaration (i.e. DTD) to allow it, so that the [XML] document will still be valid:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
[
<!ATTLIST tag myAttri CDATA #IMPLIED>
]>
#IMPLIED
means it is an optional attribute, or you could use #REQUIRED
, etc.
More information is in DTD - Attributes.
<meta http-equiv="content-type" content="application/xhtml+xml" />
. –
Welch "]>"
character in body. –
Flyman ]>
. So is there any way to make it valid without DTD or with default DTD (whatever it is in Chrome) –
Puduns data-
. –
Sybil application/xhtml+xml
) in the header. Most people that use the XHTML doctype only do so to put the browser into standards mode and aren't really serving XHTML to the browser, but HTML instead. –
Ambary You can add custom attributes to your elements at will. But that will make your document invalid.
In HTML 5 you will have the opportunity to use custom data attributes prefixed with data-
.
|
is not allowed in a css href
, but that's what's necessary for Google Fonts –
Clustered |
like so: \|
–
Estremadura %7C
–
Clustered href='https://fonts.googleapis.com/css?family=Some+Font|Another+Font|Third+Font'.replace(/\|/g, '%7C')
–
Clustered You can amend your !DOCTYPE declaration (i.e. DTD) to allow it, so that the [XML] document will still be valid:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
[
<!ATTLIST tag myAttri CDATA #IMPLIED>
]>
#IMPLIED
means it is an optional attribute, or you could use #REQUIRED
, etc.
More information is in DTD - Attributes.
<meta http-equiv="content-type" content="application/xhtml+xml" />
. –
Welch "]>"
character in body. –
Flyman ]>
. So is there any way to make it valid without DTD or with default DTD (whatever it is in Chrome) –
Puduns data-
. –
Sybil application/xhtml+xml
) in the header. Most people that use the XHTML doctype only do so to put the browser into standards mode and aren't really serving XHTML to the browser, but HTML instead. –
Ambary No, this will break validation.
In HTML 5 you can/will be able to add custom attributes. Something like this:
<tag data-myAttri="myVal" />
The jQuery data()
function allows you to associate arbitrary data with DOM elements. Here's an example.
Element.getAttribute()
, Element.setAttribute()
, or Element.dataset
developer.mozilla.org/en-US/docs/Web/API/HTMLElement/dataset –
Betaine In HTML5: yes: use the data- attribute.
<ul>
<li data-animal-type="bird">Owl</li>
<li data-animal-type="fish">Salmon</li>
<li data-animal-type="spider">Tarantula</li>
</ul>
Yes, you can do it!
Having the next HTML tag:
<tag key="value"/>
We can access their attributes with JavaScript:
element.getAttribute('key'); // Getter
element.setAttribute('key', 'value'); // Setter
Element.setAttribute()
put the attribute in the HTML tag if not exist. So, you don't need to declare it in the HTML code if you are going to set it with JavaScript.
key
: could be any name you desire for the attribute, while is not already used for the current tag.
value
: it's always a string containing what you need.
key
with data-
. –
Waylen var demo = document.getElementById("demo")
console.log(demo.dataset.myvar)
// or
alert(demo.dataset.myvar)
//this will show in console the value of myvar
<div id="demo" data-myvar="foo">anything</div>
Yes, you can, you did it in the question itself: <html myAttri="myVal"/>
.
You can set properties from JavaScript.
document.getElementById("foo").myAttri = "myVal"
use data-any , I use them a lot
<aside data-area="asidetop" data-type="responsive" class="top">
data-*
values from JavaScript? –
Congeries elem.dataset.area
in the case of the above example. For attributes with additional dashes, make the key camelCase. eg. data-box-area="30px"
becomes elem.dataset.boxArea
. –
Felsite Here is the example:
document.getElementsByTagName("html").foo="bar"
Here is another example how to set custom attributes into body tag element:
document.getElementsByTagName('body')[0].dataset.attr1 = "foo";
document.getElementsByTagName('body')[0].dataset.attr2 = "bar";
Then read the attribute by:
attr1 = document.getElementsByTagName('body')[0].dataset.attr1
attr2 = document.getElementsByTagName('body')[0].dataset.attr2
You can test above code in Console in DevTools, e.g.
Yes, you can use data-*
attribute.
The data-*
attribute is used to store custom data private to the page or application.
<ul>
<li data-pageNumber="1"> 1 </li>
<li data-pageNumber="2"> 2 </li>
<li data-pageNumber="3"> 3 </li>
</ul
With custom elements, it is common to add custom attributes without data-
prefix.
Here is an example from HTML standard: Custom elements (note the country
attribute):
<flag-icon country="nl"></flag-icon>
Another example from MDN Web Docs: Using custom elements (note the l
and c
attributes):
<custom-square l="100" c="red"></custom-square>
well! you can actually create a bunch of custom HTML attributes by disguising the data attributes in what you actually want.
eg.
[attribute='value']{
color:red;
}
<span attribute="value" >hello world</span>
It apparently works but that would invalidate your document, there is no need of using JScript for you to have custom attributes or even elements unless you have to, you just need to treat your new formulated(custom) attributes just the same way you treat your "data" attribute
Remember "invalid" does not mean anything. The document will load fine at all the time. and some browsers would actually validate your document only by the presence of DOCTYPE....., you actually know what I mean.
Another approach, which is clean and will keep the document valid, is to concatenate the data you want into another tag e.g. id, then use split to take what you want when you want it.
<html>
<script>
function demonstrate(){
var x = document.getElementById("example data").querySelectorAll("input");
console.log(x);
for(i=0;i<x.length;i++){
var line_to_illustrate = x[i].id + ":" + document.getElementById ( x[i].id ).value;
//concatenated values
console.log("this is all together: " + line_to_illustrate);
//split values
var split_line_to_illustrate = line_to_illustrate.split(":");
for(j=0;j<split_line_to_illustrate.length;j++){
console.log("item " + j+ " is: " + split_line_to_illustrate[j]);
}
}
}
</script>
<body>
<div id="example data">
<!-- consider the id values representing a 'from-to' relationship -->
<input id="1:2" type="number" name="quantity" min="0" max="9" value="2">
<input id="1:4" type="number" name="quantity" min="0" max="9" value="1">
<input id="3:6" type="number" name="quantity" min="0" max="9" value="5">
</div>
<input type="button" name="" id="?" value="show me" onclick="demonstrate()"/>
</body>
</html>
I can think of a handy use for the custom tag "init"
. Include a JavaScript expression that gets evaluated at document.onLoad()
time and provides a value for the tag, e.g.
<p><p>The UTC date is <span init="new Date().toUTCString()">?</span>.<p></p>
Some boilerplate JavaScript code would scan all the tags in the DOM at document.onload()
time looking for the init attributes, evaluating the expressions that they contain, and assigning them to the containing tag's innerHTML. This would give HTML some of the power of JSP, PHP etc. Currently we have to split the HTML markup and the JavaScript code that illuminates it. Bugs love split code.
You can add, but then you have to write a line of JavaScript code too,
document.createElement('tag');
to make sure everything fall in place. I mean Internet Explorer :)
<tag ...>
here apparently means just any HTML tag. –
Inland You can do something like this to extract the value you want from JavaScript instead of an attribute:
<a href='#' class='click'>
<span style='display:none;'>value for JavaScript</span>some text
</a>
<input type="hidden" value="...">
. Consider, though, the difference between the type of data you put in various attributes by contrast to the data you might put in a hidden field. Hiding a <span>
(of all things) in an <a>
for the sake of maintaining a piece of metadata is not a good move. It would be peculiar to your site and very much dependent on JS (graceful degradation, people). –
Filum © 2022 - 2024 — McMap. All rights reserved.