7

I am adding custom attributes to my HTMLtags something like

<li customeId="1">

I am to access this custom attribute in IE but in firefox, I am not able to get the values of these attributes. Any suggestion on how to access custom attribute in FireFox or any other way. I am using HTML 4 for development.

Code to access:

  var test =  licollection[index].customeId;

Thanks Ashwani

4
  • 1
    How are you trying to access the attribute? Add the code, so we can see what you are doing.
    – Oded
    May 13, 2010 at 6:13
  • 1
    You aren't using HTML 4. You are using tag soup. HTML 4 doesn't support custom attributes.
    – Quentin
    May 13, 2010 at 7:46
  • @David, then is there any other way to associate some properties with a tag in HTML 4?
    – Ashwani K
    May 13, 2010 at 8:39
  • Existing attributes. A script element containing some data (e.g. `var foo = { element_id: [1,2,3], other_element: [4,5,6] };). Text content.
    – Quentin
    May 13, 2010 at 8:45

5 Answers 5

18

Hopefully below code will be helpful for you.

<div id="navigation">
 <ul>
  <li customerId="1"></li>
  <li customerId="2"></li>
  <li customerId="3"></li>
 </ul>
</div>
var x = document.getElementById('navigation');
if (!x) return;
var liCollections = x.getElementsByTagName('li');
for (var i=0;i<liCollections.length;i++)
   alert(liCollections[i].getAttribute('customerid', 0));

It's clear enough, and you can understand it easily.

9

You can use HTML 5 custom data attribute functionality, it may helps you

Attribute Name

The data attribute name must be at least one character long and must be prefixed with 'data-'. It should not contain any uppercase letters.

Attribute Value

The attribute value can be any string.

Example :-

<ul id="vegetable-seeds">
  <li data-spacing="10cm" data-sowing-time="March to June">Carrots</li>
  <li data-spacing="30cm" data-sowing-time="February to March">Celery</li>
  <li data-spacing="3cm" data-sowing-time="March to September">Radishes</li>
</ul>
1
  • this is great info. just a couple questions, > what will happen if the code runs on an old browser? (no HTML5 compatibility) > is there a limit to 'data-<attrib>' tags > Sep 22, 2017 at 18:00
2
test.getAttribute('customerid');

Did you try this?

0

Try

var test = licollection[index].getAttribute("customeId");
0

licollection[index].getAttribute("customeId")

reference: http://www.java2s.com/Code/JavaScriptReference/Javascript-Methods/getAttributeSyntaxParametersandNote.htm

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.