Adding new Custom Attribute to a HTML element.

A

Alok

Hi ,
I want to know is there any way to add a custome attribute to the a
HTML element .For example ,
<INPUT type=text name="txtNAME1" id="txtNAME1" size=7 maxlength=7
displayName="dspNAME1">

In the above example the custom attribute "displayName" is not a
standard HTML attribute.
If I can define the same I can push the intended value and access
the same attribute's value through javascript .

Advance thanks for any help in this regard .

Alok
 
A

Adrienne Boswell

Hi ,
I want to know is there any way to add a custome attribute to the a
HTML element .For example ,
<INPUT type=text name="txtNAME1" id="txtNAME1" size=7 maxlength=7
displayName="dspNAME1">

In the above example the custom attribute "displayName" is not a
standard HTML attribute.
If I can define the same I can push the intended value and access
the same attribute's value through javascript .

Advance thanks for any help in this regard .

You cannot add custom attributes, but you can do what you are setting out
to do with a little javascript and server side script. There's an
example at <http://www.intraproducts.com/usenet/requiredform.asp>.
Notice the form is prefilled with values that help the user (server
side). When the user focuses on the element, the value disappears
(javascript).

This script also uses CSS to help advise the user that something is
missing or wrong. There are no tables involved, only CSS and clean
markup.
 
A

Andy Dingley

Alok said:
I want to know is there any way to add a custome attribute to the a
HTML element .

Yes, just stick them in there. It's not valid, but it's also not
harmful (so long as you avoid obviously hazardous names). There is a
long, well-established and well-supported practice in HTML that adding
unknown elements can be a problem but adding unknown attributes is
safely and quietly ignored.
 
L

Leonard Blaisdell

"Andy Dingley said:
Yes, just stick them in there. It's not valid, but it's also not
harmful (so long as you avoid obviously hazardous names). There is a
long, well-established and well-supported practice in HTML that adding
unknown elements can be a problem but adding unknown attributes is
safely and quietly ignored.

To quote a past poster that I miss, "you can't just make shit up" ;-)

leo
 
A

Andy Dingley

Leonard said:
To quote a past poster that I miss, "you can't just make shit up" ;-)

You clearly can, it's just a question of what the consequences are.

This doesn't break anything, and it's useful for some DHTML purposes. I
can't think of _ANY_ occasion when it will break a browser (any
platform).

It's not entirely portable, and it will be discarded by most HTML /
XHTML / WML transcoders. So don't expect it to work on phone networks,
and certainly code your DHTML to fail robustly if the expected
attribute unexpectedly vanishes.
 
Joined
Jan 29, 2009
Messages
1
Reaction score
0
Adding new properties and attributes

Given that so many of us want to store data in HTML elements, I sure hope that the XHTML folks find a way to accommodate that. I'm specifically talking about things like the following:

<p nref="#myAnchorID">some text to serve as a link source</p>

<p style="color:#009900; skin:green-box" id="myAnchorID">
an annotation for the link source </p>
 
Joined
Feb 15, 2010
Messages
2
Reaction score
0
well i just wrote the following html

<div id="mydiv" thing="hi"> </div>

and the following javascript worked in chrome, firefox and ie

alert(document.getElementById("mydiv").getAttribute("thing"));

...
 
Joined
Feb 15, 2010
Messages
2
Reaction score
0
well i just wrote the following html

<div id="mydiv" thing="hi"> </div>

and the following javascript worked in chrome, ie and firefox

mydiv = document.getElementById("mydiv");
alert(mydiv.getAttribute("thing"));

...
 
Joined
Mar 19, 2010
Messages
1
Reaction score
0
kewp said:
well i just wrote the following html

<div id="mydiv" thing="hi"> </div>

and the following javascript worked in chrome, firefox and ie

alert(document.getElementById("mydiv").getAttribute("thing"));

...

Verified. kewp, you tha man!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top