Ashwani K
Ashwani K

Reputation: 8000

Add custom attribute to HTML tags

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

Upvotes: 7

Views: 26264

Answers (5)

MUS
MUS

Reputation: 1450

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.

Upvotes: 18

Gaurav
Gaurav

Reputation: 8487

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>

Upvotes: 8

mangokun
mangokun

Reputation: 5633

licollection[index].getAttribute("customeId")

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

Upvotes: 0

vsync
vsync

Reputation: 130750

test.getAttribute('customerid');

Did you try this?

Upvotes: 2

kennytm
kennytm

Reputation: 523764

Try

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

Upvotes: 0

Related Questions